[英]Entity Framework Core 3.1 Code-First Model - Define model of having multiple many to many relation with self
I like to create a DB model in ASP.Net Core 3.1.我喜欢在 ASP.Net Core 3.1 中创建一个 DB model。 I am using Code First approach with EF Core 3.1.我在 EF Core 3.1 中使用 Code First 方法。
I like to create a model for this relationship-我喜欢为这种关系创建一个 model -
So, there is one Employee table and every employee has multiple bosses and each has multiple sub-ordinates.因此,有一张 Employee 表,每个员工都有多个老板,每个老板都有多个下属。 But every boss and every subordinate are employees also.但每个老板和每个下属也是员工。 What I have done is something like this-我所做的是这样的-
public class Employee
{
[HiddenInput(DisplayValue = false), Display(Name = "ID")]
[Key()]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column(Order = 1)]
public Guid? Id { get; set; } = Guid.NewGuid();
[Column("Name"), Required(ErrorMessage = "Term Name should be given"), Display(Name = "Term Name", Prompt = "Please Give Term Name")]
[DataType(DataType.Text)]
public string Name { get; set; }
public ICollection<Boss> Bosses { get; set; }
public ICollection<Subordinate> Subordinates { get; set; }
............
............
}
But I am getting this error during creating the DB model by the command Add-Migration <MigrationName>
-但是在通过命令Add-Migration <MigrationName>
创建 DB model 时出现此错误 -
Unable to determine the relationship represented by navigation property 'Employee.Bosses' of type 'ICollection'.无法确定“ICollection”类型的导航属性“Employee.Bosses”表示的关系。 Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。
Can anyone please help?有人可以帮忙吗?
direct many-to-many relations are not supported with ef core 3.1. ef core 3.1 不支持直接多对多关系。
See this: https://docs.microsoft.com/de-de/ef/core/what-is-new/ef-core-5.0/whatsnew看到这个: https://docs.microsoft.com/de-de/ef/core/what-is-new/ef-core-5.0/whatsnew
If you cannot use ef core >= 5, than you have to create a navigation property to the EmployeeBoss entity.如果您不能使用 ef core >= 5,则必须为 EmployeeBoss 实体创建导航属性。
Try this:尝试这个:
public partial class Employee
{
public Employee()
{
EmployeeBossEmployees = new HashSet<EmpoyeeBoss>();
EmployeeBossBosses = new HashSet<EmpoyeeBoss>();
}
[Key]
public int Id { get; set; }
[InverseProperty(nameof(EmpoyeeBoss.Employee))]
public virtual ICollection<EmpoyeeBoss> EmployeeBossEmployees { get; set; }
[InverseProperty(nameof(EmpoyeeBoss.Boss))]
public virtual ICollection<EmpoyeeBoss> EmployeeBossBosses { get; set; }
}
public partial class EmpoyeeBoss
{
[Key]
public int Id { get; set; }
public int BossId { get; set; }
public int EmployeeId { get; set; }
[ForeignKey(nameof(EmployeeId))]
[InverseProperty("EmployeeBossEmployees")]
public virtual Employee Employee { get; set; }
[ForeignKey(nameof(BossId))]
[InverseProperty("EmployeeBossBosses")]
public virtual Employee Boss { get; set; }
}
and include in your dbcontext:并包含在您的 dbcontext 中:
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<EmpoyeeBoss> EmployeeBosses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EmpoyeeBoss>(entity =>
{
entity.HasOne(d => d.Employee)
.WithMany(p => p.EmployeeBossEmployees)
.HasForeignKey(d => d.EmployeeId)
.OnDelete(DeleteBehavior.ClientSetNull);
entity.HasOne(d => d.Boss)
.WithMany(p => p.EmployeeBossBosses)
.HasForeignKey(d => d.BossId);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.