简体   繁体   English

使用 EF 生成的两个外键

[英]Two Foreign Keys generated with EF

I get 2 foreign keys begin generated.我开始生成 2 个外键。 How can I solve this?我该如何解决这个问题? My code is as follows:我的代码如下:

Note: the association with Person and User is 1-to-1-or-0.注意:与PersonUser的关联是 1 对 1 或 0。 A User may or may not have a Person . User可能有也可能没有Person

public class User : Entity
{
    public string Name { get; set; }
    public Person Person { get; set; }
}

public class Person: Entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public User User { get; set; }
    public ICollection<PersonSchool> PersonSchool{ get; set; }
}

Entity configuration:实体配置:

public class UserEntityConfiguration : IEntityTypeConfiguration<User>
{ 
    public void Configure(EntityTypeBuilder<User> builder)
    {
        builder.ToTable("User");
        builder.HasKey(c => c.Id);
        builder.Property(c => c.Name).IsRequired(true);
    }
}

public class PersonEntityConfiguration : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {
        builder.ToTable("Person");
        builder.HasKey(c => c.Id);
        builder.Property(c => c.FirstName).IsRequired(true);
        builder.Property(c => c.LastName).IsRequired(true);
        builder.HasOne(i => i.User)
           .WithOne()
           .HasForeignKey<Person>(rl => rl.UserId)
           .OnDelete(DeleteBehavior.Restrict);
    }
}

Result:结果:

I am copying part of the snippet from the migration code.我正在从迁移代码中复制部分代码段。 As you can see, there are 2 foreign keys created, UserId and UserId1如您所见,创建了 2 个外键, UserIdUserId1

constraints: table =>
            {
                table.PrimaryKey("PK_Person", x => x.Id);
                table.ForeignKey(
                    name: "FK_Person_User_UserId",
                    column: x => x.UserId,
                    principalTable: "User",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
                table.ForeignKey(
                    name: "FK_Person_User_UserId1",
                    column: x => x.UserId1,
                    principalTable: "User",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

I overlooked the additional navigation property User.Person that isn't properly configured.我忽略了未正确配置的附加导航属性 User.Person。 This这个

builder.HasOne(i => i.User)
   .WithOne()
   .HasForeignKey<Person>(rl => rl.UserId)
   .OnDelete(DeleteBehavior.Restrict);

should be应该

builder.HasOne(i => i.User)
   .WithOne(u => u.Person)
   .HasForeignKey<Person>(rl => rl.UserId)
   .OnDelete(DeleteBehavior.Restrict);

If you don't tell EF that User.Person is the inverse navigation property of Person.User it will configure two relationships.如果您不告诉 EF User.Person 是 Person.User 的反向导航属性,它将配置两个关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM