假设我有两个实体,User
和 Notification
:
public class User
{
[Key]
public int UserId { get; set; }
// other public properties
// Navigation Properties
public virtual ICollection<Notification> Notifications { get; set; }
}
public class Notification
{
[Key]
public int NotificationId { get; set; }
// other public properties
// Navigation Properties
public virtual ICollection<User> Users { get; set; }
}
我只是添加多对多
关系,如下所示:
modelBuilder.Entity<User>()
.HasMany(u => u.Notifications)
.WithMany(t => t.Users)
.Map(m =>
{
m.ToTable("UserNotifications");
m.MapLeftKey("UserId");
m.MapRightKey("NotificationId");
});
但是,如果我想向该表添加名称为 'Status' 的额外列,会发生什么情况?我以为我可以使用 AddColumn
但是我如何访问此列并获取它的值?我怎样才能做到这一点?我想检查用户是否阅读了通知。任何建议都会很棒,谢谢。
最佳答案
您无法在关系中添加“状态”等额外字段。您需要有一个虚拟表。
我认为您的问题与以下答案相同
Many to Many mapping with extra fields in Fluent API
也许这会有所帮助。
User
Notification
UserNotification (dummy table) where you put your Status field
关于c# - 如何向 Entity Framework 中的第三个表添加额外的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20673785/