Entity Framework Fluent API是用来配置领域类,以重写默认约定的。EF Fluent API是基于Fluent API设计模式的(也就是Fluent接口),Fluent API是通过方法链式调用来配置实体的。
在EF 6中,DBModelBuilder类就充当Fluent API,我们可以使用它来配置很多东西。Fluent API提供了比数据注解更多的配置选项。
为了使用Fluent API的配置,我们需要在上下文类中,重写DbContext类的OnModelCreating方法,例如:
public class SchoolContext: DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Write Fluent API configurations here
}
}
你可以同时使用数据注解特性和Fluent API。Entity Framework优先使用Fluent API的配置,而不是数据注解。
在EF 6中,Fluent API配置模型的以下方面:
下面的图形列出来了Fluent API重要的方法。
后面的章节中,我们开始学习使用Fluent API配置实体吧。
10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
原文:https://www.cnblogs.com/caofangsheng/p/10682533.html