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

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

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

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

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()方法用来配置类型。

配置可空、不可空的列

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

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数据类型的属性的精度。

配置并发列

你可以使用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[]数组类型的属性上。

最新文章

  1. js防止客户端多触发
  2. ecshop 后台模板设置-》设置模板
  3. VirtualBox安装Ubuntu教程
  4. 通过JDBC连接hive
  5. NSInternalInconsistencyException: loaded the &quot;XXXView&quot; nib but the view outlet was not set
  6. LeetCode 213
  7. google map api 学习笔记
  8. 代码与编程(java基础)
  9. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)
  10. manifold tangent classifier
  11. python 2解决编码问题
  12. 使用 dep 配置 golang 开发环境
  13. Biorhythms POJ - 1006 中国剩余定理
  14. 剑指offer(3)从尾到头打印链表
  15. 使用pandas的部分问题汇总
  16. Docker一些常用命令
  17. [翻译]NUnit---TearDown and SetUpFixture and Test Attributes(二十)
  18. coursera课程Text Retrieval and Search Engines之Week 3 Overview
  19. Variation Model的应用
  20. DocX操作word生成报表

热门文章

  1. python全栈开发day28-网络编程之粘包、解决粘包,上传和下载的作业
  2. 037 关于pom.xml的一些问题的理解
  3. C#资源管理器
  4. FastAdmin 的上传代码在哪里?
  5. python小工具myqr生成动态二维码
  6. C++雾中风景番外篇3:GDB与Valgrind ,调试代码内存的工具
  7. Python3 Srcapy 爬虫
  8. ACM差分约束笔记
  9. iOS 技术篇:__VA_ARGS__实现自定义NSLog
  10. BZOJ.3064.CPU监控(线段树 历史最值)