EF Core 3 1对0关系问题

EF Core 3 1 to 0 relationship problems

我正在尝试定义客户可以拥有 0 个或 1 个地址的关系。对于这种关系,我想要 Addresses table 中的 customerId,但我不想要 Customer table 中的 addressId。听起来很简单?我查看了样本,但在 OnModelCreating 中缺少 hasForeignKey 或 hasOptional 等属性,因此 none 我查看的其他样本有效。我正在使用 EF Core 3.15 版。无论我尝试什么,迁移都会向 Customer table 添加一个 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");
    }

我想,这个例子可以解决你的问题。

https://anthonygiretti.com/2018/01/11/entity-framework-core-2-table-splitting/

首先,将你的AddressEntityCustomerEntity写成如下:

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);
}

现在生成一个全新的迁移,希望一切都按预期生成。