Ada*_*son 4 c# ef-fluent-api 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; }
}
Run Code Online (Sandbox Code Playgroud)
public class ApplicationUser : IdentityUser
{
public IEnumerable<Property> properties { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
上面是我的课程,我已经运行了迁移,生成了这个属性表。
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);
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它正在创建正确的 UserId 列和外键。但它也产生一个ApplicationUserId,我无法弄清楚是什么原因造成的。
任何想法?
任何帮助,将不胜感激。
在你流畅的 API 配置中
public void Configure(EntityTypeBuilder<ApplicationUser> builder) {
builder.HasMany<Property>()
.WithOne(p => p.User)
.HasForeignKey(f => f.UserId);
}
Run Code Online (Sandbox Code Playgroud)
ApplicationUser您只是说和实体之间存在一对多关系Property,但没有说明哪些成员涉及多方。Property事实上,和之间可能存在多种关联ApplicationUser。
要使其正常工作,请通过调整配置来指定两端涉及的成员,如下所示
public void Configure(EntityTypeBuilder<ApplicationUser> builder) {
builder.HasMany(u => u.properties)
.WithOne(p => p.User)
.HasForeignKey(f => f.UserId);
}
Run Code Online (Sandbox Code Playgroud)