EF CodeFirst关系映射约定与Fluent API

Entity Framework Code First关系映射约定

一.本篇随笔目录:

1、外键列名默认约定

2、一对多关系

3、一对一关系

4、多对多关系

5、一对多自反关系

6、多对多自反关系

(…,具体看链接)
https://www.cnblogs.com/libingql/p/3353112.html#3

二.Fluent API

除了惯例原则与属性数据注解外,FluentAPI是另一种支持实体类配置设置的方式。与属性数据注解相比,它提供了更广泛的功能与设置弹性。实体类若同时设置了数据注解,则采用的优先权是“Fluent API” > “数据注解” > “惯例”。一旦设置了Fluent API 无论数据注解还是惯例规则均会被覆盖。

DbContext类定义的OnModelCreating 方法是最常调用FLuent API的地方,如下图所示:

public class SchoolContext: DbContext 
{
    public KTStoreContext():base("name=KTStore")
    {
        
    }
 
    public DbSet<Product> Products { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Write Fluent API configurations here
 
    }
}

在这里插入图片描述
(…)
https://blog.csdn.net/xuchen_wang/article/details/98874127

posted @ 2021-05-26 21:05  有诗亦有远方  阅读(20)  评论(0)    收藏  举报  来源