Asp.net core 通过 modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted"); 添加软删除但作为复合键被删除

问题描述

我插入软删除标志

modelBuilder.Entity<Dependenciadisciplina>().Property<bool>("isDeleted");

但我需要将其添加为复合键

有人可以帮我吗?

解决方法

我插入软删除标志

modelBuilder.Entity<DependenciaDisciplina>().Property<bool>("isDeleted");

但我需要将其添加为复合键

set the Composite Keys使用 Fluent API,我们必须使用 HasKey() 方法。

查看HasKey()方法定义后,我们可以看到参数应该是字符串数组或表达式。

enter image description here

因此,您可以像这样使用设置复合键(将 Car 模型更改为您的模型):

 modelBuilder.Entity<Car>().Property<bool>("isDeleted"); 
 modelBuilder.Entity<Car>().HasKey(new string[] { "CarId","isDeleted" });

汽车模型:

public class Car
{
    [Key]
    public int CarId { get; set; }
    public string CarName { get; set; }
    public string LicensePlate { get; set; } 
    public string Make { get; set; }
    public string Model { get; set; }
}

然后,迁移之后,结果是这样的:

enter image description here