本文介绍了我怎样才能在实体框架中添加额外的列到第三个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个实体,用户通知

Assume i have two entities, User and 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");
            });



但会发生什么,如果我想要额外的列到这个表格,它的名字叫状态?我以为我可以使用 AddColumn 但我怎么可以访问此列,得到它的价值呢?我怎样才能做到这一点?我要检查用户是否已读通知与否。任何建议将是巨大的,谢谢。

But what happens if i want to add extra column to this table which name is 'Status' ? I thought i can use AddColumn but then how can i access this column and get it's value ? How can i accomplish this? I want to check whether the user read notification or not. Any suggestions would be great,thanks.

推荐答案

您不能添加额外的字段一样的关系状态。你需要有一个虚拟表的。
,我觉得你的问题是一样的如下回答结果的
额外的字段很多映射可能,这将有助于。

You can't add extra field like "Status" in the relationship. You need to have a dummy table for that.
And i think your question is same as the following answer
Many to Many mapping with extra fields in Fluent APIMay be this would help.

用户

通知

UserNotification(虚拟表),你把你的状态字段

这篇关于我怎样才能在实体框架中添加额外的列到第三个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 01:25