Movies Project
In this section, a Movie
model class and a Db
database session class will be added into Movies project.
Project Setup
Make the following changes to Movies project:
- Delete Class1.cs (or Class1.vb if you're using VB.Net) from the project.
- Add NuGet Package DevZest.Data.SqlServer to this project.
Add Movie Class
Step 1. Add new class
Add new class Movie
into project Movies, inherits from Model:
The class can be viewed in Model Visualizer tool window:
Note
You can show Model Visualizer tool window by clicking menu "View" -> "Other Windows" -> "Model Visualizer" in Visual Studio.
Step 2. Add ID field
In Model Visualizer tool window, click the left top button, the following code snippet will be inserted:
Tabbing through the code snippet to change the property name to ID
and property type to _Int32
. When done, press ESC to quit code snippet editing. You now have a readonly property ID
as type _Int32
, with a compile-time warning Missing registration for property 'ID'
. You can fix this warning by moving the caret to the property name, and pressing CTRL-. in Visual Studio:
Select Add Mounter _ID in the dropdown menu, RDO.Tools will automatically insert following code to register the property:
Step 3. Annotate ID field
In Model Visualizer tool window, right click ID field, available annotations for current field will be displayed as context menu:
Click Identity, field ID
will be annotated with Identity
attribute:
using DevZest.Data;
using DevZest.Data.Annotations;
namespace Movies
{
public class Movie : Model
{
public static readonly Mounter<_Int32> _ID = RegisterColumn((Movie _) => _.ID);
[Identity]
public _Int32 ID { get; private set; }
}
}
Step 4. Add primary key
In Model Visualizer tool window, click the left top dropdown button, then click Add Primary Key... from the drop down menu:
The following dialog will be displayed:
Field ID
is detected automatically. Click OK button in the dialog, the following code will be generated automatically:
...
public class Movie : Model<Movie.PK>
{
public sealed class PK : CandidateKey
{
public PK(_Int32 id) : base(id)
{
}
}
protected sealed override PK CreatePrimaryKey()
{
return new PK(ID);
}
public class Key : Key<PK>
{
static Key()
{
Register((Key _) => _.ID, _ID);
}
protected sealed override PK CreatePrimaryKey()
{
return new PK(ID);
}
public _Int32 ID { get; private set; }
}
...
}
Step 5. Add other fields
Add following fields using steps described previously:
Name | Type | Annotation(s) |
---|---|---|
Title | _String | [StringLength(60, MinimumLength = 3)] , [Required] , [SqlNVarChar(60)] |
ReleaseDate | _DateTime | Display(Name = "Release Date")] , [SqlDate] , [Required] |
Genre | _String | [RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")] , [Required] , [StringLength(30)] , [SqlNVarChar(30)] |
Price | _Decimal | [SqlMoney] , [Required] |
Note
Please keep in mind Model Visualizer is your best friend. It provides much more features which are not demonstrated in this tutorial for simplicity.
The final Movie class:
using DevZest.Data;
using DevZest.Data.Annotations;
using DevZest.Data.SqlServer;
namespace Movies
{
public class Movie : Model<Movie.PK>
{
public sealed class PK : CandidateKey
{
public PK(_Int32 id) : base(id)
{
}
}
protected sealed override PK CreatePrimaryKey()
{
return new PK(ID);
}
public class Key : Key<PK>
{
static Key()
{
Register((Key _) => _.ID, _ID);
}
protected sealed override PK CreatePrimaryKey()
{
return new PK(ID);
}
public _Int32 ID { get; private set; }
}
public static readonly Mounter<_Int32> _ID = RegisterColumn((Movie _) => _.ID);
public static readonly Mounter<_String> _Title = RegisterColumn((Movie _) => _.Title);
public static readonly Mounter<_DateTime> _ReleaseDate = RegisterColumn((Movie _) => _.ReleaseDate);
public static readonly Mounter<_String> _Genre = RegisterColumn((Movie _) => _.Genre);
public static readonly Mounter<_Decimal> _Price = RegisterColumn((Movie _) => _.Price);
[Identity]
public _Int32 ID { get; private set; }
[StringLength(60, MinimumLength = 3)]
[Required]
[SqlNVarChar(60)]
public _String Title { get; private set; }
[Display(Name = "Release Date")]
[SqlDate]
[Required]
public _DateTime ReleaseDate { get; private set; }
[RegularExpression(@"^[A-Z]+[a-zA-Z""'\s-]*$")]
[Required]
[StringLength(30)]
[SqlNVarChar(30)]
public _String Genre { get; private set; }
[SqlMoney]
[Required]
public _Decimal Price { get; private set; }
}
}
Add Db Class
Add new class
Add new class Db
into project Movies, inherits from SqlSession:
using DevZest.Data.SqlServer;
using System.Data.SqlClient;
namespace Movies
{
public partial class Db : SqlSession
{
public Db(string connectionString)
: this(new SqlConnection(connectionString))
{
}
public Db(SqlConnection sqlConnection)
: base(sqlConnection)
{
}
}
}
The class can be viewed in Db Visualizer tool window:
Note
You can show Db Visualizer tool window by clicking menu "View" -> "Other Windows" -> "Db Visualizer" in Visual Studio.
Add table property
In Db Visualizer tool window, click the left top button, the following dialog will be displayed:
Select Movie from Model: combo box, then click button OK, a Movie
property will be generated in class Db
:
using DevZest.Data;
using DevZest.Data.SqlServer;
using System.Data.SqlClient;
namespace Movies
{
public partial class Db : SqlSession
{
public Db(string connectionString)
: this(new SqlConnection(connectionString))
{
}
public Db(SqlConnection sqlConnection)
: base(sqlConnection)
{
}
private DbTable<Movie> _movie;
public DbTable<Movie> Movie
{
get
{
return GetTable(ref _movie);
}
}
}
}
Note
Please keep in mind Db Visualizer is your best friend. It provides additional features which are not demonstrated in this tutorial for simplicity.