【问题标题】:How to create a third table for Many to Many relationship in Entity Framework Core?如何在 Entity Framework Core 中为多对多关系创建第三个表?
【发布时间】:2018-09-10 13:23:11
【问题描述】:

我有两个课程: 一是用户

 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。 我怎样才能做到这一点?

【问题讨论】:

  • 这是一个多对多关联。取决于您拥有哪些选项的 EF 版本。但应该不难找到例子。
  • @GertArnold,我认为你是对的,它是多对多关系,因为一个订阅可以与多个用户相关联,但鉴于 Subscription 类不包含用户列表,如何你在 EF 中处理这个?

标签: c# entity-framework


【解决方案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。您不需要在导航属性中使用 virtual,因为延迟加载 still not available in EF Core

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 当我向当前用户添加新订阅时会发生什么?该条目将放在哪个表中?
    • @Sameed 我编辑了我的解决方案,因为第一个解决方案不是一个非常可靠的解决方案。检查新的两个解决方案
    • 在第一个解决方案的情况下,EF 是否可以在没有我指定另一个类的情况下自行创建第三个表?对于第二种解决方案,如果为用户添加新订阅,它将在 User 表中创建一个新条目,我认为这不是一件好事,因为所有其他列数据都会重复?
    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 2019-06-18
    • 2020-01-18
    • 2020-07-22
    • 2016-12-18
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    相关资源
    最近更新 更多