正在为id EF Core 3.1创建重复的外键和列



遇到ef core 3.1为另一个表中的id字段创建重复列的问题。我目前有一个从IdentityUser继承的ApplicationUser实体,以及一个将ApplicationUser id存储为UserId的属性实体。

public class Property
{
public Guid PropertyId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Guid AddressId { get; set; }
public Address PropertyAddress { get; set; }
public bool IsHome { get; set; }
public string UserId { get; set; }
public ApplicationUser User { get; set; }
}
public class ApplicationUser : IdentityUser
{
public IEnumerable<Property> properties { get; set; }
}
public class PropertyConfig : IEntityTypeConfiguration<Property>
{
public void Configure(EntityTypeBuilder<Property> builder)
{
builder.HasKey(p => p.PropertyId);
builder.HasOne(p => p.PropertyAddress).WithOne();
builder.HasOne(p => p.User).WithMany(p => p.properties).HasForeignKey(f => f.UserId).OnDelete(DeleteBehavior.Restrict);
}
}
public class ApplicationUserConfig : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
builder.ToTable("User");
builder.HasKey(p => p.Id);
builder.HasMany<Property>().WithOne(p => p.User).HasForeignKey(f => f.UserId);
}
}

上面是我的类,我已经运行了迁移,它为属性生成了这个表。

migrationBuilder.CreateTable(
name: "Property",
columns: table => new
{
PropertyId = table.Column<Guid>(nullable: false),
Name = table.Column<string>(nullable: true),
Description = table.Column<string>(nullable: true),
AddressId = table.Column<Guid>(nullable: false),
IsHome = table.Column<bool>(nullable: false),
UserId = table.Column<string>(nullable: true),
ApplicationUserId = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Property", x => x.PropertyId);
table.ForeignKey(
name: "FK_Property_Address_AddressId",
column: x => x.AddressId,
principalTable: "Address",
principalColumn: "AddressId",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Property_User_ApplicationUserId",
column: x => x.ApplicationUserId,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_Property_User_UserId",
column: x => x.UserId,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});

正如您所看到的,它正在创建一个UserId列和外键,这是正确的。但它也产生了ApplicationUserId,我不知道是什么原因造成的。

有什么想法吗?

如有任何帮助,我们将不胜感激。

在流畅的API配置中

public void Configure(EntityTypeBuilder<ApplicationUser> builder) {
builder.HasMany<Property>()
.WithOne(p => p.User)
.HasForeignKey(f => f.UserId);
}

你只是说ApplicationUserProperty实体之间存在一对多关系,但不是说哪些成员来自方。实际上,在PropertyApplicationUser之间可能存在多个关联。

要使其发挥作用,请通过调整您的配置来指定两端涉及的成员,如

public void Configure(EntityTypeBuilder<ApplicationUser> builder) {
builder.HasMany(u => u.properties)
.WithOne(p => p.User)
.HasForeignKey(f => f.UserId);
}

这是因为两者都有:

public string UserId { get; set; }
public ApplicationUser User { get; set; }

如果你不想让User引用User表,你就必须忽略它

builder.Property(p => p.User).Ignore();

但这意味着当您从上下文中获取Property时,User将为null。此外,如果将ApplicationUser附加到Property,然后保存属性,则User和relantiship都不会写入数据库,并且需要手动设置UserId

相关内容

  • 没有找到相关文章

最新更新