你的位置:首页 > ASP.net教程

[ASP.net教程]使用Fluent API进行属性映射【Code


现在,我打算学习,怎么用Fluent API来配置领域类中的属性。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class Student  {    public int StudentKey { get; set; }    public string StudentName { get; set; }    public int StuaentAge { get; set; }    public string StudentEmail { get; set; }    public Standard Standard { get; set; }  }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class Standard  {    public int StandardKey { get; set; }    public int StandardName { get; set; }    public ICollection<Student> Students { get; set; }  }}

请注意上面的代码中,Student,和Standard实体中的标注颜色的属性字段,我没有使用类名+ID或者ID的写法。而是使用了自定义的方式,这样Code-First默认约定就不知道,他们两个是主键列了,除非手动配置。

一、配置主键和复合主键【联合主键】

可以使用EntityTypeConfiguration类里面的HasKey方法。请注意,modelbuilder.Entity<TEntity>()泛型方法,返回的是EntityTypeConfiguration对象。

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using System.Data.Entity.ModelConfiguration.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class DBContextClass:DbContext  {    public DBContextClass() : base("ConnectionStrings") { }    public DbSet<Student> Students { get; set; }    //public DbSet<Standard> Standards { get; set; }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {                //配置主键:      modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);      modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);      base.OnModelCreating(modelBuilder);    }  }}

 


using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using System.Data.Entity.ModelConfiguration.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class DBContextClass:DbContext  {    public DBContextClass() : base("ConnectionStrings")    {      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());    }    public DbSet<Student> Students { get; set; }    //public DbSet<Standard> Standards { get; set; }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {                //配置主键:      //modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);      modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);      //配置复合主键(联合主键)      modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });      base.OnModelCreating(modelBuilder);    }  }}

 

 

得到的数据库是:

二、配置列名,列类型、列顺序

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using System.Data.Entity.ModelConfiguration.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class DBContextClass:DbContext  {    public DBContextClass() : base("ConnectionStrings")    {      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());    }    public DbSet<Student> Students { get; set; }    //public DbSet<Standard> Standards { get; set; }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {                //配置主键:      //modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);      modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);      //配置复合主键(联合主键)      modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });      //配置列名,列类型,列顺序      modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName("年龄").HasColumnOrder(1).HasColumnType("int");      base.OnModelCreating(modelBuilder);    }  }}

生成的数据库:

modelBuilder.Entity<TEntity>().Property(expression) allows you to use different methods to configure a particular property, as shown below.

modelBuilder.Entity<TEntity>().Property(expression)这个方法,允许你配置指定的属性。

 

 

三、配置列是可空还是不可空

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using System.Data.Entity.ModelConfiguration.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class DBContextClass:DbContext  {    public DBContextClass() : base("ConnectionStrings")    {      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());    }    public DbSet<Student> Students { get; set; }    //public DbSet<Standard> Standards { get; set; }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {                //配置主键:      //modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);      modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);      //配置复合主键(联合主键)      modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });      //配置列名,列类型,列顺序      modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName("年龄").HasColumnOrder(1).HasColumnType("int");      //可空列      modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsOptional();      base.OnModelCreating(modelBuilder);    }  }}

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration;using System.Data.Entity.ModelConfiguration.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF4{  public class DBContextClass:DbContext  {    public DBContextClass() : base("ConnectionStrings")    {      Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DBContextClass>());    }    public DbSet<Student> Students { get; set; }    //public DbSet<Standard> Standards { get; set; }    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {                //配置主键:      //modelBuilder.Entity<Student>().HasKey(s => s.StudentKey);      modelBuilder.Entity<Standard>().HasKey(s => s.StandardKey);      //配置复合主键(联合主键)      modelBuilder.Entity<Student>().HasKey(s => new { s.StudentKey, s.StudentName });      //配置列名,列类型,列顺序      modelBuilder.Entity<Student>().Property(s => s.StuaentAge).HasColumnName("年龄").HasColumnOrder(1).HasColumnType("int");      //可空列     // modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsOptional();      //不可空      modelBuilder.Entity<Standard>().Property(s => s.StandardName).IsRequired();      base.OnModelCreating(modelBuilder);    }  }}

 后面本来已经,翻译好了,结果,网络不稳定,没备份,,现在直接贴英文了!!!

Configure Column Size:

Code-First will set the maximum size of a data type for a column. You can override this convention, as shown below.

namespace CodeFirst_FluentAPI_Tutorials{      public class SchoolContext: DbContext   {    public SchoolDBContext(): base()     {    }    public DbSet<Student> Students { get; set; }    public DbSet<Standard> Standards { get; set; }        protected override void OnModelCreating(DbModelBuilder modelBuilder)    {      //Set StudentName column size to 50      modelBuilder.Entity<Student>()          .Property(p => p.StudentName)          .HasMaxLength(50);                  //Set StudentName column size to 50 and change datatype to nchar       //IsFixedLength() change datatype from nvarchar to nchar      modelBuilder.Entity<Student>()          .Property(p => p.StudentName)          .HasMaxLength(50).IsFixedLength();                  //Set size decimal(2,2)        modelBuilder.Entity<Student>()          .Property(p => p.Height)          .HasPrecision(2, 2);    }  }}    

As you can see in the above example, we used HasMaxLength method to set the size of a column. IsFixedLength method converts nvarchar to nchar type. In the same way, HasPrecision method changed the precision of the decimal column.

Configure Concurrency Column:

You can configure a property as concurrency column using ConcurrencyToken method, as shown below.

namespace CodeFirst_FluentAPI_Tutorials{      public class SchoolContext: DbContext   {    public SchoolDBContext(): base()     {    }    public DbSet<Student> Students { get; set; }    public DbSet<Standard> Standards { get; set; }        protected override void OnModelCreating(DbModelBuilder modelBuilder)    {      //Set StudentName as concurrency column      modelBuilder.Entity<Student>()          .Property(p => p.StudentName)          .IsConcurrencyToken();    }  }}    

As you can see in the above example, we set StudentName column as concurrency column so that it will be included in the where clause in update and delete commands.

You can also use IsRowVersion() method for byte[] property to make it as a concurrency column.