EF Core 3 1对0关系问题

坦率

我正在尝试定义一个客户可以拥有0或1个地址的关系。对于这种关系,我希望在“地址”表中使用customerId,但不希望在“客户”表中使用addressId。听起来很简单?我已经看过示例,但是在OnModelCreating中缺少了诸如hasForeignKey或hasOptional之类的某些属性,因此我没有看过其他示例。我正在使用EF Core 3.15版。不管我尝试什么,迁移都会向“客户”表添加一个addressId列。我希望能够使用sql语句“从地址删除”删除所有地址

这是我的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");
    }
雷杰普·塞利

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

EF Core 3.0 1:0与流利的关系

来自分类Dev

从 EF Core 2 迁移到 EF Core 3

来自分类Dev

对EF Core 2.1关系的怀疑

来自分类Dev

映射到关系类型时EF Core 3添加迁移失败

来自分类Dev

EF Core 3 Casting无法翻译

来自分类Dev

EF Core 3 Linq无法翻译

来自分类Dev

在EF Core 3中映射json

来自分类Dev

我不想要的EF Core加载关系

来自分类Dev

EF Core 5,删除多对多关系

来自分类Dev

EF Core 关系 - 冗余列 id

来自分类Dev

EF Core 3.1中的TimeSpan问题

来自分类Dev

将列映射到EF Core 3中的子对象

来自分类Dev

如何在EF Core 3查询中解析int?

来自分类Dev

Xamarin Forms,Sqlite,EF Core 3,迁移和大量混淆

来自分类Dev

EF Core 3:配置导航属性的后备字段

来自分类Dev

EF Core 3,优化很多包含/然后包含

来自分类Dev

EF Core返回的子对象不超过3个

来自分类Dev

表格中多个1:1关系的EF Core 3.1.7数据注释

来自分类Dev

定义实体之间的主键关系时,EF Core 3.1将创建名称为“ 1”的重复列

来自分类Dev

这样做有效吗:EF Core 3.1,多个(0..1)对1关系

来自分类Dev

EF Core中按ID和类型的关系

来自分类Dev

EF Core流利API中的多对多关系

来自分类Dev

不必要地在EF Core中加载关系

来自分类Dev

EF Core 在设置关系时设置为 null

来自分类Dev

.NET Core EF Select 对象,其中关系具有

来自分类Dev

EF Core 2.0 中的种子一对多关系

来自分类Dev

一对一的反身关系 ef core

来自分类Dev

.NET Core 3 EF中不存在没有联合实体的MN关系,也没有关于为什么的文档

来自分类Dev

.NET Core / EF.Core 3+将控制台日志添加到DbContext