c# - 如何向 Entity Framework 中的第三个表添加额外的列?

标签 c# entity-framework ef-code-first entity-relationship

假设我有两个实体,UserNotification:

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/

相关文章:

entity-framework - Entity Framework - 代码优先模型验证

.net - EntityFunctions.DiffHours 方法如何工作?

c# - ASP.NET Core appsettings.json reloadOnChange 不适用于符号链接(symbolic link)文件?

c# - .NET C# switch 语句字符串比较与枚举比较

c# - JQuery 和不可见的选择 - JQuery

c# - 在 Entity Framework 中将两个外键定义为主键

c# - 如何使用 Entity Framework 使用外键保存新记录?

c# - 如何从外部 DLL 中保存复杂对象?

entity-framework - 如何使用流畅的配置在 Entity Framework 6.2 中创建索引

c# - DropDownList,获取C#中返回的DataValueField