10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】

原文连接:https://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspxhtml

EF 6 Code-First系列文章目录:typescript

Fluent API能够配置实体中的属性,将其映射为数据表中的列。使用Fluent API,你能够改变相应列的名称、数据类型、大小、NULL、Not NUll、主键、外键、以及并发列等等。数据库

咱们将使用下面的实体类进行配置:api

public class Student
{
    public int StudentKey { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Standard Standard { get; set; }
}
    
public class Standard
{
    public int StandardKey { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}
配置主键以及复合主键

上面的实体代码,并不遵循Code-First约定【对于主键】,由于他们没有名称为Id的属性或者没有{实体名称}+”Id“的属性。因此你能够,使用HasKey()方法配置主键,请记住,modelBuilder.Entity<TEntity>()方法返回的是EntityTypeConfiguration类型的对象。数组

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) {
        //Configure primary key
        modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
        modelBuilder.Entity<Standard>().HasKey<int>(s => s.StandardKey);

        //Configure composite primary key
        modelBuilder.Entity<Student>().HasKey<int>(s => new { s.StudentKey, s.StudentName }); 
    }
}
配置列的名称、类型、顺序

默认的Code-FIrst约定,建立和属性同样的列名,顺序,以及数据类型,你能够重写这个约定:并发

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) {
        //Configure Column
        modelBuilder.Entity<Student>()
                    .Property(p => p.DateOfBirth)
                    .HasColumnName("DoB")
                    .HasColumnOrder(3)
                    .HasColumnType("datetime2");
    }
}

正如上面代码所示,Property()方法用来配置实体中的属性,HasColumnName()方法用来配置列名,HasColumnOrder()用来配置顺序,HasColumnType()方法用来配置类型。
enter description hereapp

配置可空、不可空的列

EF 6 API会为原始数据类型的属性,建立Not NULL列,由于原始数据类型不能为空,除非标识了可空的符号?或者Nullable.
使用IsOptional()方法建立可空列,一样使用IsRequired()方法建立不可空列。ide

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) {
            //Configure Null Column
        modelBuilder.Entity<Student>()
                .Property(p => p.Heigth)
                .IsOptional();
                        
            //Configure NotNull Column
            modelBuilder.Entity<Student>()
                .Property(p => p.Weight)
                .IsRequired();
    }
}
配置列的大小

Code-First默认会为列建立最大的大小(nvarchar(max)或者varchar(max)),你能够重写这个约定:测试

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);
    }
}

正如上面代码所示,咱们使用HasMaxLength()方法配置列的大小,IsFixedLength()方法会将列的类型从nvarchar转到nchar.HasPrecision()能够配置decimal数据类型的属性的精度。ui

配置并发列

你能够使用ConcurrencyToken()方法配置并发列,例如:

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();
    }
}

上面的代码中,StudentName列是并发列,因此更新和删除的时候,这个列名将会在where子句中。 你一样能够使用IsRowVersion()来配置并发列,只不过这个IsRowVersion()只能用在byte[]数组类型的属性上。

相关文章
相关标签/搜索