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,我无法弄清楚是什么原因造成的。
任何想法?
任何帮助,将不胜感激。