繁体   English   中英

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

[英]How to create a third table for Many to Many relationship in Entity Framework Core?

我有两节课:一是用户

 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; }
    }

如您所见,该用户具有订阅列表。 现在,当使用实体框架代码第一种方法时,我将获得一个用户表,该表不包含订阅,但是将用户ID的新列添加到订阅表中。 我期望有第三个表,其中包含两列,一列具有用户ID,另一列具有订阅ID。 我该如何实现?

创建第三个中间表,例如: 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; }
}

文档

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

所以这个答案是正确的。

我只是更正了一点代码:

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。 您无需在导航属性中使用virtual,因为在EF Core中仍然无法进行延迟加载。

暂无
暂无

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

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