繁体   English   中英

ef core 2.1多对多

[英]ef core 2.1 Many To Many

我正在尝试在EF Core 2.1中创建4个表及其关系

一个产品中有一个或多个角色。 例如,制作“汉密尔顿”中有一个导演角色,

Production
ProductionId     Name        
------------     -----------
PR1               Hamilton

Person
PersonId         Name
--------         -----------
P1               Rick
P2               Chris
P3               Dan

Role
RoleId           Name
----------       -----------
R1               Director
R2               Choreographer
R3               Actor

ProductionRolePerson
ProductionId    PersonId    RoleId
------------    --------    ------
PR1             P1 (Rick)   R1 (Director)
PR1             P2 (Chris)  R2 (Choreographer)
PR1             P3 (Dan)    R3 (Actor)


Production 
    | 1
    | *
ProductionPersonRole
| *             | *
| 1             | 1
Person         Role

我想不出正确的方法来建立所有一对多关系。

这是我设置的课程:

 public class Production 
    {
        public int ProductionId { get; set; }
        public List<ProductionPersonRole> Roles { get; set; }
    }

    public class Person 
    {
        public int PersonId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public ICollection<ProductionPersonRole> ProductionPersonRoles { get; set; }
    }

public class Role
    {
        public int RoldId { get; set; }
        public string Name {get;set;}
        public ICollection<ProductionPersonRole> Jobs { get; set; }
    }

public class ProductionPersonRole
    {
        public Production Production { get; set; }
        public Person Person { get; set; }
        public Role Role { get; set; }
    }

这是DbContext类:

    public class MyContext : DbContext
        {
            public DbSet<Production> Productions { get; set; }
            public DbSet<ProductionPersonRole> ProductionPersonRoles { get; set; }
            public DbSet<Role> Roles { get; set; }
            public DbSet<Person> Persons { get; set; }

            public MyContext(): base()
            {

            }

            public MyContext(DbContextOptions options): base(options)
            {

            }

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                modelBuilder.Entity<ProductionPersonRole>()
                    .HasKey(ppr => new {ppr.Production.ProductionId, ppr.Person.PersonId, ppr.Role.RoldId});

                modelBuilder.Entity<ProductionPersonRole>()
                    .HasOne(ppr => ppr.Production)
                    .WithMany(ppr => ppr.Roles)
                    .HasForeignKey(ppr => ppr.Production.ProductionId);

                modelBuilder.Entity<ProductionPersonRole>()
                    .HasOne(ppr => ppr.Person)
                    .WithMany(ppr => ppr.ProductionPersonRoles)
                    .HasForeignKey(ppr => ppr.Person.PersonId);

                modelBuilder.Entity<ProductionPersonRole>()
                    .HasOne(ppr => ppr.Role)
                    .WithMany(ppr => ppr.Jobs)
                    .HasForeignKey(ppr => ppr.Role.RoleId);

                base.OnModelCreating(modelBuilder);
            }
}

我朝正确的方向前进吗?

是的,只需验证数据库体系结构即可。 因为如果不正确,EF将创建具有不同体系结构的数据库。

设计还可以。 但是流畅的配置却不是。 EF Core不允许在Fluent API中使用嵌套属性,因此您使用HasKeyHasForeignKey方法将导致运行时异常。

由于您的模型符合EF Core约定,并且所有关系都通过导航属性进行了描述,因此EF Core将能够自动确定几乎所有内容,而无需任何流畅的配置,但连接实体组合主键除外。 这是唯一需要流畅配置的软件。

虽然可以仅使用阴影属性来配置复合PK(知道FK属性命名的EF Core约定,例如public Person Person int PersonId > int PersonId ):

modelBuilder.Entity<ProductionPersonRole>()
     .HasKey("ProductionId", "PersonId", "RoleId");

我建议将显式FK属性添加到联接实体:

public class ProductionPersonRole
{
    public int ProductionId { get; set; }
    public int PersonId { get; set; }
    public int RoleId { get; set; }
    public Production Production { get; set; }
    public Person Person { get; set; }
    public Role Role { get; set; }
}

并改用以下配置:

modelBuilder.Entity<ProductionPersonRole>()
     .HasKey(ppr => new { ppr.ProductionId, ppr.PersonId, ppr.RoleId });

它不仅避免了使用硬编码的字符串,而且在断开连接的情况下插入/删除链接时也非常有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM