【问题标题】:ASP.NET Core EF Many to Many referencing tableASP.NET Core EF 多对多引用表
【发布时间】:2016-05-24 16:41:11
【问题描述】:

我正在尝试使用联系人表创建用户。我不确定我的做法是否正确,因为添加了一个我没有声明的列。

实体:

public class User
{
    public int Id { get; set; }
    public bool IsAvailable { get; set; }
    public List<Contact> Contacts { get; set; }
}

public class Contact
{
    public int UserId { get; set; }
    public int ContactUserId { get; set; }

    public User User { get; set; }
    public User ContactUser { get; set; }
}

映射:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Contact>()
        .HasKey(x => new { x.UserId, x.ContactUserId });

    modelBuilder.Entity<Contact>()
        .HasOne(x => x.User)
        .WithMany(x => x.Contacts)
        .HasForeignKey(x => x.UserId);

    modelBuilder.Entity<Contact>()
        .HasOne(x => x.ContactUser)
        .WithMany(x => x.Contacts)
        .HasForeignKey(x => x.ContactUserId);
}

结果:

migrationBuilder.CreateTable(
    name: "Contact",
    columns: table => new
    {
        UserId = table.Column<int>(nullable: false),
        ContactUserId = table.Column<int>(nullable: false),
        UserId1 = table.Column<int>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Contact", x => new { x.UserId, x.ContactUserId });
        table.ForeignKey(
            name: "FK_Contact_User_ContactUserId",
            column: x => x.ContactUserId,
            principalTable: "User",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_Contact_User_UserId1",
            column: x => x.UserId1,
            principalTable: "User",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    });

真正的问题:

联系人中的 UserId1 列来自哪里?我的定义有问题吗?谢谢!

【问题讨论】:

  • 您要建立 n:n 或 1:n 关系吗?从您的模型看来,您有 1 个用户和 n 个联系人,如果我没记错的话,这不是多对多而是一对多。
  • @HotTowelie 它是 1:n 到 Contact,然后是 n:1 到 Contact.ContactUser。所以,User 是 n:n 到 它自己
  • 可能相关:User.Contacts 不能同时是从 User 到其 ContactUsers 的导航属性 同时向另一方向移动的导航属性.其中一个应该从User 实体中配置出来,另一个配置为无参数HasMany()

标签: c# asp.net asp.net-core entity-framework-core


【解决方案1】:

您最终在联系人表上添加了一个 UserId1 的原因是因为您指定了 UserContactUser 关联的另一端将联系人对象上的 Contacts 放在用户对象上,这是不正确的。结果,EF 忽略它并为用户对象上的 Contacts 创建另一个关联,并将其映射到联系人表上的 UserId1 列。

解决此问题的一种方法是在用户对象上创建另一个联系人列表并相应地映射它:

public class User
{
    public int Id { get; set; }
    public bool IsAvailable { get; set; }

    public List<Contact> Contacts { get; set; }
    public List<Contact> ContactUsers { get; set; }
}

modelBuilder.Entity<Contact>()
    .HasOne(x => x.User)
    .WithMany(x => x.Contacts)
    .HasForeignKey(x => x.UserId);

modelBuilder.Entity<Contact>()
    .HasOne(x => x.ContactUser)
    .WithMany(x => x.ContactUsers)
    .HasForeignKey(x => x.ContactUserId)
    .OnDelete(DeleteBehavior.Restrict);


这会产生所需的架构:

【讨论】:

  • 非常感谢,也为 OnDelete 添加点赞!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
  • 2020-07-14
  • 1970-01-01
  • 2019-08-24
相关资源
最近更新 更多