C# 如何在实体框架中向第三个表添加额外的列?
假设我有两个实体,C# 如何在实体框架中向第三个表添加额外的列?,c#,entity-framework,ef-code-first,entity-relationship,C#,Entity Framework,Ef Code First,Entity Relationship,假设我有两个实体,用户和通知: public class User { [Key] public int UserId { get; set; } // other public properties // Navigation Properties public virtual ICollection<Notification> Notifications { get; set; } } public class 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; }
}
但是,如果我想向这个名为“Status”的表中添加额外的列,会发生什么呢?我想我可以使用
AddColumn
,但是我如何访问此列并获取其值呢?我怎样才能做到这一点?我想检查用户是否阅读了通知。任何建议都很好,谢谢。您不能在关系中添加“状态”之类的额外字段。你需要有一个虚拟的表格。我想你的问题和下面的答案是一样的
也许这会有帮助
用户通知
UserNotification(虚拟表)用于放置状态字段的位置
我不知道为什么你不能添加列并映射它。。。我对fluentapi和映射了解不多,只是对如何从上下文访问该专栏感到困惑
modelBuilder.Entity<User>()
.HasMany(u => u.Notifications)
.WithMany(t => t.Users)
.Map(m =>
{
m.ToTable("UserNotifications");
m.MapLeftKey("UserId");
m.MapRightKey("NotificationId");
});