简体   繁体   English

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

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

I have two classes: One is User 我有两节课:一是用户

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

Other is Subscription: 其他是订阅:

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

As you can see that User has a list of Subscriptions. 如您所见,该用户具有订阅列表。 Now when using the entity framework code first approach I am getting a table for User which doesn't contain Subscriptions but a new column for User Id is being added to Subscription table. 现在,当使用实体框架代码第一种方法时,我将获得一个用户表,该表不包含订阅,但是将用户ID的新列添加到订阅表中。 I was expecting to have a third table which contains two columns one with User ID and the other with subscription ID. 我期望有第三个表,其中包含两列,一列具有用户ID,另一列具有订阅ID。 How can I achieve this? 我该如何实现?

Create a third middle table named: UserSubscriptions for example. 创建第三个中间表,例如: 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; }
}

Second Solution: 第二种解决方案:

Add reference for Subscription to User and name it CurrentSubscription for example. 例如,将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; }
}

From documentation : 文档

Many-to-many relationships without an entity class to represent the join table are not yet supported. 尚不支持没有实体类来表示联接表的多对多关系。 However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships. 但是,可以通过为联接表包括一个实体类并映射两个单独的一对多关系来表示多对多关系。

So this answer is correct. 所以这个答案是正确的。

I just corrected code a little bit: 我只是更正了一点代码:

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. PS。 You don't need use virtual in navigation property, because lazy loading still not available in EF Core . 您无需在导航属性中使用virtual,因为在EF Core中仍然无法进行延迟加载。

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

相关问题 实体框架核心创建多对多关系 - Entity Framework Core Creating a Many to Many Relationship 如何在Entity Framework Core中配置多对多关系 - How to configure a Many To Many relationship in Entity Framework Core 如何在 Entity Framework Core 中更新多对多关系中的表? - How to update tables in many-to-many relationship in Entity Framework Core? ASP .NET Core MVC Entity Framework - 如何访问由多对多关系产生的表? - ASP .NET Core MVC Entity Framework - How to get access to a table which is result of many-to-many relationship? 如何在Entity Framework Core中将项目添加到多对多关系表中? - How to add an item to a many-to-many relationship table in Entity Framework Core? 如果数据库没有链接表,如何在Entity Framework中创建多对多关系? - How do you create a many-to-many relationship in Entity Framework if the database doesn't have a linking table? 如何在 Entity Framework Core 中创建多对多关系 - How to create many to many relationships in Entity Framework Core 实体框架中有多少关系正在发挥作用 - How many to many relationship in Entity Framework is working 如何与实体框架建立多对多的关系 - How to make many to many relationship with entity framework 如何与实体框架实现多对多关系? - How to implement many to many relationship with Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM