假设我有两个实体,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; }
}
我只是在添加many-to-many
关系,像这样:
modelBuilder.Entity<User>()
.HasMany(u => u.Notifications)
.WithMany(t => t.Users)
.Map(m =>
{
m.ToTable("UserNotifications");
m.MapLeftKey("UserId");
m.MapRightKey("NotificationId");
});
但是,如果我想向该表添加名称为“状态”的额外列怎么办?我以为可以使用,AddColumn
但是我如何才能访问此列并获得其价值?我怎样才能做到这一点?我想检查用户是否阅读通知。任何建议都很好,谢谢。
您不能在关系中添加诸如“状态”之类的额外字段。您需要为此创建一个虚拟表。
我认为您的问题与以下答案相同:
在Fluent API中使用额外字段进行多对多映射可能会有所帮助。
用户
通知
UserNotification(虚拟表),您在其中放置了状态字段
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句