Entity Framework Core 实体关系的配置

Posted JimCarter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Entity Framework Core 实体关系的配置相关的知识,希望对你有一定的参考价值。

https://docs.microsoft.com/zh-cn/ef/core/modeling/relationships

1. 术语介绍

  • Dependent entity: 依赖实体,包含外键的实体,实体关系中的子。
  • Principal entity: 主体实体,包含主键/备用键的实体,实体关系中的父。
  • Principal key: 主键/备用键
  • Foreign key: 外键
  • Navigation property: 导航属性,出现在Dependent entity 或Principal entity上的实体,用来指向相关的其它实体。
    • Collection navigation property:是多个对象的集合
    • Reference navigation property:单个对象
    • Inverse navigation property:反向导航属性
  • Self-referencing relationship:自引用的关系,如树形结构中的id和parentId

参考以下代码:

public class Blog//主体实体
{
    public int BlogId { get; set; }//主键
    public string Url { get; set; }

    public List<Post> Posts { get; set; }//导航属性,也是Post.Blog的反向导航属性
}

public class Post//依赖实体
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }//外键
    public Blog Blog { get; set; }//导航属性,也是Blog.Posts的反向导航属性
}

2. 自动配置的关系

如果实体类的一个属性无法映射为标量类型(scalar type, 如int、long等),则认为是导航属性。如果在某个实体类上发现了导航属性,将会创建一个关系。

2.1 完整配置

如上一小节的Post和Blog的定义,都是像那样定义比较完备的。定义完备有两点要求:

  • 实体两边有对应的导航属性,而非一边有一边没有
  • 如果“依赖实体”中某个属性的名称符合以下规则,则被配置为外键:
    • <navigation property name><principal key property name>
    • <navigation property name>Id
    • <principal entity name><principal key property name>
    • <principal entity name>Id

2.2 无外键情况下进行配置

虽然建议要有外键,但是没有外键也ok。当没外键时会引入名称符合<navigation property name><principal key property name><principal entity name><principal key property name>规则的影子属性作为外键。

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    
    public Blog Blog { get; set; }
}

此时会产生一个名为BlogId的影子属性。

2.3 无导航属性对情况下配置

即使没有对应的反向导航属性,没有外键属性,但只要存在一个导航属性就能建立起关系

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

3. 手动配置关系

使用HasOne/HasMany配置导航属性和使用WithOne/WithMany配置反向导航属性。

internal class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Post>()
            .HasOne(p => p.Blog)//表示一个Post对应一个Blog
            .WithMany(b => b.Posts);//表示一个Blog对应多个Post
            
        //上述代码等效于下面这两行
        modelBuilder.Entity<Post>().HasOne(p=>p.Blog);
        modelBuilder.Entity<Blog>().HasMany(b=>b.Posts);
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}

当然也可以通过特性配置:

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int AuthorUserId { get; set; }
    public User Author { get; set; }

    public int ContributorUserId { get; set; }
    public User Contributor { get; set; }
}

public class User
{
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [InverseProperty("Author")]
    public List<Post> AuthoredPosts { get; set; }

    [InverseProperty("Contributor")]
    public List<Post> ContributedToPosts { get; set; }
}

看下建表语句都是什么:

CREATE TABLE "SysRole" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_SysRole" PRIMARY KEY AUTOINCREMENT,
    "RoleName" TEXT NOT NULL
);

CREATE TABLE "SysUsers" (
    "Id" INTEGER NOT NULL CONSTRAINT "PK_SysUsers" PRIMARY KEY AUTOINCREMENT,
    "UserName" TEXT NOT NULL,
    "Pwd" TEXT NOT NULL,
    "CreateTime" TEXT NULL,
    "RoleId" INTEGER NULL,
    CONSTRAINT "FK_SysUsers_SysRole_RoleId" FOREIGN KEY ("RoleId") REFERENCES "SysRole" ("Id") ON DELETE RESTRICT
);

3.1 无导航属性对下的配置

如果你的导航属性不是成对的,则WithOneWithMany可以不传入参数。

internal class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasMany(b => b.Posts)
            .WithOne();
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
}

3.2 手动指定外键

使用FluentAPI可以配置单一外键和复合外键:

internal class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Post>()
            .HasOne(p => p.Blog)
            .WithMany(b => b.Posts)
            .HasForeignKey(p => p.BlogForeignKey);
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogForeignKey { get; set; }
    public Blog Blog { get; set; }
}

复合外键:

internal class MyContext : DbContext
{
    public DbSet<Car> Cars { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Car>()
            .HasKey(c => new { c.State, c.LicensePlate });//复合主键

        modelBuilder.Entity<RecordOfSale>()
            .HasOne(s => s.Car)
            .WithMany(c => c.SaleHistory)
            .HasForeignKey(s => new { s.CarState, s.CarLicensePlate });//复合外键
    }
}

public class Car
{
    public string State { get; set; }
    public string LicensePlate { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    public List<RecordOfSale> SaleHistory { get; set; }
}

public class RecordOfSale
{
    public int RecordOfSaleId { get; set; }
    public DateTime DateSold { get; set; }
    public decimal Price { get; set; }

    public string CarState { get; set; }
    public string CarLicensePlate { get; set; }
    public Car Car { get; set; }
}

可以使用特性配置单一外键:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogForeignKey { get; set; }

    [ForeignKey("BlogForeignKey")]//如果BlogForeignKey不存在,则会创建影子属性
    public Blog Blog { get; set; }
}

配置影子外键:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
        //1.首先配置影子属性
        modelBuilder.Entity<Post>()
            .Property<int>("BlogForeignKey");

        //2.然后将影子属性配置成外键
        modelBuilder.Entity<Post>()
            .HasOne(p => p.Blog)
            .WithMany(b => b.Posts)
            .HasForeignKey("BlogForeignKey");
}

配置外键的名称:默认情况下外键的名称规则为FK _<依赖实体的类名>_<主体实体的类名>_<外键属性的名称>可以在数据库中看到,如FK_SysUsers_SysRole_RoleId
当然也可以手动更改外键的名称:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Post>()
        .HasOne(p => p.Blog)
        .WithMany(b => b.Posts)
        .HasForeignKey(p => p.BlogId)
        .HasConstraintName("ForeignKey_Post_Blog");
}

3.3 无导航属性下的配置

internal class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Post>()
            .HasOne<Blog>()
            .WithMany()
            .HasForeignKey(p => p.BlogId);
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
}

3.4 将外键指向到非主键上

如果希望外键指向到非实体主键的属性上。则可以如下配置:

  1. 单一外键
internal class MyContext : DbContext
{
    public DbSet<Car> Cars { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RecordOfSale>()
            .HasOne(s => s.Car)
            .WithMany(c => c.SaleHistory)
            .HasForeignKey(s => s.CarLicensePlate)
            .HasPrincipalKey(c => c.LicensePlate);
    }
}

public class Car
{
    public int CarId 以上是关于Entity Framework Core 实体关系的配置的主要内容,如果未能解决你的问题,请参考以下文章

Entity Framework Core中更改跟踪工作原理

Entity Framework Core中更改跟踪工作原理

如何将动态实体设置为 dbcontext Entity Framework Core?

什么是自有实体?何时以及为什么在 Entity Framework Core 中使用 Owned Entity?

Entity Framework Core - 如何处理相关实体映射和保存

更新 ASP.NET Core Entity Framework 中的实体类