我试图定义一个关系,在这个关系中,一个客户可以有0或1个地址。对于这种关系,我希望在Addresses表中有customerId,但我不希望在Customer表中有addressId。听起来很简单?我看了一些样本,但是在OnModelCreating中缺少一些属性,比如hasForeignKey或者hasOptional,所以我看的其他样本都不能用。我使用的是EF Core 3.15版本。无论我怎么尝试,迁移都是在Customer表中添加一个addressId列。我希望能够用sql语句 "delete from addresses "删除所有地址。
以下是我的2个实体
public class AddressEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid AddressId { get; set; }
public string Line1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public virtual Guid Customer { get; set; }
}
public class CustomerEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }
public virtual AddressEntity Address { get; set; }
}
以下是我的迁移代码
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Addresses",
columns: table => new
{
AddressId = table.Column<Guid>(nullable: false),
Line1 = table.Column<string>(nullable: true),
City = table.Column<string>(nullable: true),
State = table.Column<string>(nullable: true),
PostalCode = table.Column<string>(nullable: true),
Country = table.Column<string>(nullable: true),
Customer = table.Column<Guid>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Addresses", x => x.AddressId);
});
migrationBuilder.CreateTable(
name: "Customers",
columns: table => new
{
CustomerId = table.Column<Guid>(nullable: false),
CustomerName = table.Column<string>(nullable: true),
CustomerEmail = table.Column<string>(nullable: true),
AddressId = table.Column<Guid>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Customers", x => x.CustomerId);
table.ForeignKey(
name: "FK_Customers_Addresses_AddressId",
column: x => x.AddressId,
principalTable: "Addresses",
principalColumn: "AddressId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Customers_AddressId",
table: "Customers",
column: "AddressId");
}
首先,写下你的 AddressEntity
和 CustomerEntity
如下所示。
public class AddressEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid CustomerId { get; set; }
public string Line1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public CustomerEntity Customer { get; set; }
}
public class CustomerEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustomerEmail { get; set; }
public AddressEntity Address { get; set; }
}
现在,在 OnModelCreating
如下图所示。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomerEntity>()
.HasOne(c => c.Address)
.WithOne(a => a.Customer)
.HasForeignKey<AddressEntity>(a => a.CustomerId);
}
现在产生一个全新的迁移,希望一切都能如期产生。