如何在Entity Framework Core中为多对多关系创建第三个表?

时间:2018-03-31 21:10:08

标签: c# entity-framework

我有两节课: 一个是用户

 public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public List<Subscription> Subscriptions { get; set; }
    }

其他是订阅:

public class Subscription
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

正如您所看到的,User有一个订阅列表。 现在,当使用实体框架代码第一种方法时,我得到一个用户表,它不包含订阅,但是用户ID的新列正被添加到订阅表。我希望有一个第三个表,其中包含两列,一列是用户ID,另一列是订阅ID。 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

创建一个名为UserSubscriptions的第三个中间表,例如。

public class User
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public virtual ICollection<UserSubscription> Subscriptions { get; set; }
    }


public class Subscription
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

  public class UserSubscription
{
    public int ID { get; set; }
    public int SubscriptionID { get; set; }
    public decimal Price { get; set; }
    public int UserID { get; set; }
    public virtual User { get; set; }
    public DateTime BeginDate { get; set; }
    public DateTime EndDate { get; set; }
}

第二个解决方案:

Subscription的引用添加到User,并将其命名为CurrentSubscription

public class User
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public int CurrentSubscriptionID { get; set; }
        public virtual Subscription Subscription { get; set; }
    }


public class Subscription
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

答案 1 :(得分:1)

来自documentation

尚不支持没有实体类来表示连接表的多对多关系。但是,您可以通过包含连接表的实体类并映射两个单独的一对多关系来表示多对多关系。

所以this answer是正确的。

我刚刚纠正了一些代码:

class MyContext : DbContext
{
    public DbSet<Use> Users { get; set; }
    public DbSet<Subscription> Subscriptions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserSubscription>()
            .HasKey(t => new { t.UserId, t.SubscriptionId });

        modelBuilder.Entity<UserSubscription>()
            .HasOne(pt => pt.User)
            .WithMany(p => p.UserSubscription)
            .HasForeignKey(pt => pt.UserId);

        modelBuilder.Entity<UserSubscription>()
            .HasOne(pt => pt.Subscription)
            .WithMany(t => t.UserSubscription)
            .HasForeignKey(pt => pt.SubscriptionId);
    }
}

public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public List<UserSubscription> UserSubscriptions{ get; set; }
    }

public class Subscription
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public List<UserSubscription> UserSubscriptions{ get; set; }
    }

public class UserSubscription
{
    public int UserId { get; set; }
    public User User { get; set; }

    public int SubscriptionId { get; set; }
    public Subscription Subscription { get; set; }
}

PS。您不需要在导航属性中使用虚拟,因为延迟加载still not available in EF Core