【问题标题】:How can i add extra column to third table in Entity Framework?如何在实体框架的第三个表中添加额外的列?
【发布时间】:2013-12-19 05:01:03
【问题描述】:

假设我有两个实体,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; }

}

我只是像这样添加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");
            });

但是,如果我想在这个名为 'Status' 的表中添加额外的列,会发生什么?我以为我可以使用AddColumn,但是我怎样才能访问此列并获得它的价值?我怎样才能做到这一点?我想检查用户是否阅读了通知。任何建议都会很棒,谢谢。

【问题讨论】:

  • 不确定我是否理解您为什么不能只添加列并对其进行映射? ...
  • 我对 Fluent API 和映射了解不多,我只是对如何从我的上下文中访问该列感到困惑

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


【解决方案1】:

您不能在关系中添加额外的字段,例如“状态”。您需要为此准备一张虚拟桌子。
而且我认为您的问题与以下答案相同
Many to Many mapping with extra fields in Fluent API 可能这会有所帮助。

用户
通知
您放置状态字段

的用户通知(虚拟表)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 2015-09-15
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多