| |
 |
Lesson 1 - Creating The Project. Scaffolding Wizard.
In this lesson you will start creating the sample application built according the MVVM pattern and learn how to use the Scaffolding Wizard.
-
Create a new WinForms application. To create your project's Data Model, add a new Model folder and use the code snippets below to create three classes – Account, Transaction and Category. These classes use Data Annotation Attributes and require the System.ComponentModel.DataAnnotations library to be included in your project.
Account Data Model
C# |
using System.ComponentModel.DataAnnotations;
namespace MVVMExpenses.DataModels {
public class Account {
[Key, Display(AutoGenerateField = false)]
public long ID { get; set; }
[Required, StringLength(30, MinimumLength = 4)]
[Display(Name = "ACCOUNT")]
public string Name { get; set; }
[DataType(DataType.Currency)]
[Display(Name = "AMOUNT")]
public decimal Amount { get; set; }
public override string ToString() {
return Name + " (" + Amount.ToString("C") + ")";
}
public virtual ICollection<Transaction> Transactions { get; set; }
}
}
|
VB |
Imports System.ComponentModel.DataAnnotations
Namespace MVVMExpenses.DataModels
Public Class Account
<Key, Display(AutoGenerateField := False)>
Public Property ID() As Long
<Required, StringLength(30, MinimumLength := 4), Display(Name := "ACCOUNT")>
Public Property Name() As String
<DataType(DataType.Currency), Display(Name := "AMOUNT")>
Public Property Amount() As Decimal
Public Overrides Function ToString() As String
Return Name & " (" & Amount.ToString("C") & ")"
End Function
Public Overridable Property Transactions() As ICollection(Of Transaction)
End Class
End Namespace
|
Category Data Model
C# |
using System.ComponentModel.DataAnnotations;
namespace MVVMExpenses.DataModels {
public class Category {
[Key, Display(AutoGenerateField = false)]
public long ID { get; set; }
[Required, StringLength(30, MinimumLength = 5)]
[Display(Name = "CATEGORY")]
public string Name { get; set; }
[EnumDataType(typeof(TransactionType))]
[Display(Name = "TRANSACTION TYPE")]
public TransactionType Type { get; set; }
public override string ToString() {
return Name + " (" + Type.ToString() + ")";
}
public virtual ICollection<Transaction> Transactions { get; set; }
}
}
|
VB |
Namespace MVVMExpenses.DataModels
Public Class Category
<Key, Display(AutoGenerateField := False)>
Public Property ID() As Long
<Required, StringLength(30, MinimumLength := 5), Display(Name := "CATEGORY")>
Public Property Name() As String
<EnumDataType(GetType(TransactionType)), Display(Name := "TRANSACTION TYPE")>
Public Property Type() As TransactionType
Public Overrides Function ToString() As String
Return Name & " (" & Type.ToString() & ")"
End Function
Public Overridable Property Transactions() As ICollection(Of Transaction)
End Class
End Namespace
|
Transaction Data Model
C# |
using System.ComponentModel.DataAnnotations;
namespace MVVMExpenses.DataModels {
public enum TransactionType {
Expense,
Income
}
public class Transaction {
[Key, Display(AutoGenerateField = false)]
public long ID { get; set; }
[Display(AutoGenerateField = false)]
public long AccountID { get; set; }
[Display(Name = "ACCOUNT")]
public virtual Account Account { get; set; }
[Display(AutoGenerateField = false)]
public long CategoryID { get; set; }
[Display(Name = "CATEGORY")]
public virtual Category Category { get; set; }
[DataType(DataType.Date)]
[Display(Name = "DATE")]
public DateTime Date { get; set; }
[DataType(DataType.Currency)]
[Display(Name = "AMOUNT")]
public decimal Amount { get; set; }
[DataType(DataType.MultilineText)]
[Display(Name = "COMMENT")]
public string Comment { get; set; }
}
}
|
VB |
Namespace MVVMExpenses.DataModels
Public Enum TransactionType
Expense
Income
End Enum
Public Class Transaction
<Key, Display(AutoGenerateField := False)>
Public Property ID() As Long
<Display(AutoGenerateField := False)>
Public Property AccountID() As Long
<Display(Name := "ACCOUNT")>
Public Overridable Property Account() As Account
<Display(AutoGenerateField := False)>
Public Property CategoryID() As Long
<Display(Name := "CATEGORY")>
Public Overridable Property Category() As Category
<DataType(DataType.Date), Display(Name := "DATE")>
Public Property [Date]() As Date
<DataType(DataType.Currency), Display(Name := "AMOUNT")>
Public Property Amount() As Decimal
<DataType(DataType.MultilineText), Display(Name := "COMMENT")>
Public Property Comment() As String
End Class
End Namespace
|
-
Go to the Visual Studio's Project menu and select the Manage NuGet Packages... menu item. Search for the Entity Framework 6 package in the online storage and click 'Install'. Note that without this NuGet package installed, you will be unable to work with entity models in this sample application.
-
Add the following DbContext class descendant that will serve as the context for the DevExpress Scaffolding Wizard.
C# |
using System.Data.Entity;
using MVVMExpenses.DataModels;
namespace MVVMExpenses.DataBase {
public class MyDbContext : System.Data.Entity.DbContext {
static MyDbContext() {
System.Data.Entity.Database.SetInitializer<MyDbContext>(null);
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Transaction> Transactions { get; set; }
}
}
|
VB |
Imports System.Data.Entity
Imports MVVMExpenses.DataModels
Namespace MVVMExpenses.DataBase
Public Class MyDbContext
Inherits System.Data.Entity.DbContext
Shared Sub New()
System.Data.Entity.Database.SetInitializer(Of MyDbContext)(Nothing)
End Sub
Public Property Accounts() As DbSet(Of Account)
Public Property Categories() As DbSet(Of Category)
Public Property Transactions() As DbSet(Of Transaction)
End Class
End Namespace
|
-
Before proceeding to the next step, build your project and make sure it compiles without errors. Then, right-click your project in the Visual Studio Solution Explorer and select Add DevExpress Item - Data Model Scaffolding... (see the figure below). This will launch the Scaffolding Wizard, which will generate the complete application hierarchy, including the MVVM-ready DataModel from your data context.

Note
If this menu item is missing, right-click your project and select the default Visual Studio Add - New Item... option. In the dialog, select the DevExpress Template Gallery and manually launch the Scaffolding Wizard - choose the Data Model Scaffolding tab (the WinForms Data Models group) and click the Run Wizard button in the bottom right corner.
In the Wizard dialog, select the context created in the second step and click Next. Check all required tables and views, then click Finish to launch model generation.

-
As a result, you will see many new files, folders and classes added to your application.

Do not panic, you will never have to touch most of these files. At this moment, only two folders generated by the Wizard are worth mentioning - the DataModel folder that contains your CRUD data model and the ViewModels folder that contains MVVM ViewModels for each table within the DbContext - Account, Category and Transaction. In the following lesson, you will learn how to create Views for these ViewModels.

See Also
Is this topic helpful?
Additional Feedback
Close
|