原文連接: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()方法用來配置類型。
app
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[]數組類型的屬性上。