gpt4 book ai didi

c# - 为 ID EF Core 3.1 创建的重复外键和列

转载 作者:行者123 更新时间:2023-12-04 15:23:30 25 4
gpt4 key购买 nike

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);
}
}
以上是我的类(class),我已经运行了生成此属性表的迁移。
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);
}
您只是说 ApplicationUser 之间存在一对多关系。和 Property实体,而不是从多方面涉及哪些成员。确实, Property之间可能存在多个关联。 ApplicationUser .
为了让它工作,通过像这样调整你的配置来指定两端涉及的成员
public void Configure(EntityTypeBuilder<ApplicationUser> builder) {
builder.HasMany(u => u.properties)
.WithOne(p => p.User)
.HasForeignKey(f => f.UserId);
}

关于c# - 为 ID EF Core 3.1 创建的重复外键和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com