[英]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.