简体   繁体   English

Entity Framework Core 3.1 Code-First Model - 定义与 self 具有多个多对多关系的 model

[英]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 -

ERD

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-我所做的是这样的-

Employee Model-员工模型-

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.

相关问题 如何在Entity Framework代码优先方法中映射自我的递归关系 - How to map recursive relation on self in Entity Framework code-first approach 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship 实体框架核心中的一对多关系 - One to many relation in Entity Framework Core Entity Framework代码优先:如何在模型上添加更多道具并更新数据库? - Entity Framework code-first : how to add more props on model and update database? 如何基于Entity Framework代码优先模型自动生成.mdf db(如果不存在) - How to automatically generate .mdf db if it does not exist based on Entity Framework code-first model Build \\ Create mvc5模型(代码优先),具有多个元素的固定数量一对多(N-arry?)的关系 - Build\Create Model of mvc5 (code-first) with a relationship of one to many (N-arry?) of FIXED number of the many elements 实体框架-手动向多对多关系添加属性-模型优先 - Entity Framework - Manually Add Properties to Many-to-Many Relationship - Model First 实体框架多对多关系,无法传入模型 - Entity Framework many-to-many relationship, cannot pass in model 实体框架多对多关系错误 - Entity Framework many to many relation error 实体框架(代码优先),多对多,用户和角色 - Entity framework (code first), many to many, users and roles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM