English 中文(简体)
实体框架 核心—— 一至一的关系不产生外国关键
原标题:Entity Framework Core - One-to-one relationships not creating foreign key

I m in .net8 ef core with postgres database and Code first approach.

我有两个类别:

    public class Soggetto 
    {
        public long Id { get; set; }

        public virtual Azienda Azienda { get; set; }
        public long AziendaId { get; set; }
    }

    public class Azienda
    {
        public long Id { get; set; }

        public virtual Soggetto Soggetto { get; set; }
        public long SoggettoId { get; set; }
    }

以及这两个流体组合:

    public class SoggettoConfiguration : BaseAziendaAnagraficaEntityConfiguration<Soggetto>
    {
        public override void Configure(EntityTypeBuilder<Soggetto> builder)
        {
            base.Configure(builder);

            builder
                .HasOne(soggetto => soggetto.Azienda)
                .WithOne(azienda => azienda.Soggetto)
                .HasForeignKey<Soggetto>(x => x.AziendaId);
        }      
    }

    public class AziendaConfiguration : BaseEntityConfiguration<Azienda>
    {
        public override void Configure(EntityTypeBuilder<Azienda> builder)
        {
            base.Configure(builder);


            builder
                .HasOne(azienda => azienda.Soggetto)
                .WithOne(soggetto => soggetto.Azienda)
                .HasForeignKey<Azienda>(azienda => azienda.SoggettoId);
        }
    }

So when EF generate code for migration, I find foreign key to Soggetto in Azienda table and no foreign key to Azienda in Soggetto table. My expectation is to find both FK: SoggettoId in Azienda table and AziendaId in Soggetto table.

任何想法?

http://www.un.org。

澄清

Azienda与租户一样,每张桌有一个租户 • 确定谁是记录所有人。 Soggetto与人一样,可以是人、交货盖、客户、供应商和承租人。 i 系指名称、电子邮件和其他信息。

因此,每一个Soggetto(例如,人)都有一个Azienda(主人)来确认所有者(例如,他们是我的客户)。 另一方面,Azienda(主人)有一个名字、电子邮件和其他信息储存在Soggetto。

问题回答

首先,需要明确解释这种关系。

在你目前的模式中,要求举行全民公投,以建立一个索夫雷(因为该id不能完全取消)。

反之,要求设立“Azienda”。

这是一种矛盾。 不能首先创造。

Edited

鉴于提问者所作的澄清,看来:

  • Azienda represents a Tenant.
  • Every Soggetto (Subject) should be linked to an Azienda to denote ownership.

从实体框架(EF)模式来看,这一点如下:

www.un.org/Depts/DGACM/index_spanish.htm 如果Azienda能够有多个贫民窟:

public class Azienda
{
    public long Id { get; set; }
    public List<Soggetto> Soggettos { get; set; }
}
public class Soggetto
{
    public long Id { get; set; }

    public virtual Azienda Azienda { get; set; }
    public long AziendaId { get; set; }

    internal class Configuration : IEntityTypeConfiguration<Soggetto>
    {
        public void Configure(EntityTypeBuilder<Soggetto> builder)
        {
            builder
                .HasOne(o => o.Azienda)
                .WithMany(o => o.Soggettos);
        }
    }
}

移民法典:

        migrationBuilder.CreateTable(
            name: "Azienda",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1")
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Azienda", x => x.Id);
            });

        migrationBuilder.CreateTable(
            name: "Soggetto",
            columns: table => new
            {
                Id = table.Column<long>(type: "bigint", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                AziendaId = table.Column<long>(type: "bigint", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Soggetto", x => x.Id);
                table.ForeignKey(
                    name: "FK_Soggetto_Azienda_AziendaId", 
                    column: x => x.AziendaId,
                    principalTable: "Azienda",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Soggetto_AziendaId",
            table: "Soggetto",
            column: "AziendaId");

www.un.org/Depts/DGACM/index_spanish.htm 如果Azienda只能有一个Soggetto:

public class Azienda
{
    public long Id { get; set; }
    public Soggetto Soggetto { get; set; }
}
public class Soggetto
{
    public long Id { get; set; }

    public virtual Azienda Azienda { get; set; }
    public long AziendaId { get; set; }

    internal class Configuration : IEntityTypeConfiguration<Soggetto>
    {
        public void Configure(EntityTypeBuilder<Soggetto> builder)
        {
            builder
                .HasOne(o => o.Azienda)
                .WithOne(o => o.Soggetto);
        }
    }
}

移民法典:

migrationBuilder.CreateTable(
    name: "Azienda",
    columns: table => new
    {
        Id = table.Column<long>(type: "bigint", nullable: false)
            .Annotation("SqlServer:Identity", "1, 1")
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Azienda", x => x.Id);
    });

migrationBuilder.CreateTable(
    name: "Soggetto",
    columns: table => new
    {
        Id = table.Column<long>(type: "bigint", nullable: false)
            .Annotation("SqlServer:Identity", "1, 1"),
        AziendaId = table.Column<long>(type: "bigint", nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Soggetto", x => x.Id);
        table.ForeignKey(
            name: "FK_Soggetto_Azienda_AziendaId",
            column: x => x.AziendaId,
            principalTable: "Azienda",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

migrationBuilder.CreateIndex(
    name: "IX_Soggetto_AziendaId",
    table: "Soggetto",
    column: "AziendaId",
    unique: true); // <- set foreign key unique.

}

我认为,你对一对一的关系有很小的误解(或我误解了你的问题)。 基本内容见欧洲核心:

必要的关系确保每个附属实体必须与某些主要实体联系起来。 However,一个主要实体永远不存在任何附属实体。 也就是说,必要的关系并不表明总是有附属实体。 欧洲复兴开发银行模式中没有任何办法,在关系数据库中也没有标准方式确保本金与附属公司相关。 如果需要,则必须根据(企业)逻辑加以实施。 详情见

如果你想模拟“真实的”一对一的关系,唯一的选择是使用自有实体类型,如前联的“Required Navigations”部分所述:

There is one exception to this rule - when the principal and dependent types are sharing the same table in a relational database, or contained in a document. This can happen with owned types, or non-owned types sharing the same table. In this case, the navigation property from the principal to the dependent can be marked as required, indicating that the dependent must exist.

If you have several one-to-one relationships between tables (i.e. Soggetto can have a depended Azienda and Azienda can have another depended Soggetto) then you will need to have several properties. For example something along these lines (not tested):

public class Soggetto 
{
    public long Id { get; set; }

    public virtual Azienda ChildAzienda { get; set; }

    public long? ParentAziendaId { get; set; }
    public virtual Azienda? ParentAzienda { get; set; }
}

public class Azienda 
{
    public long Id { get; set; }

    public long ParentSoggettoId { get; set; }
    public virtual Soggetto ParentSoggetto { get; set; }

    public virtual Soggetto ChildSoggetto { get; set; }
}

并相应地将其与挥发性反应器绘制成图。 例如:

public class SoggettoConfiguration : IEntityTypeConfiguration<Soggetto>
{
    public void Configure(EntityTypeBuilder<Soggetto> builder)
    {
        builder
            .HasOne(soggetto => soggetto.ChildAzienda)
            .WithOne(azienda => azienda.ParentSoggetto)
            ;
    }      
}

public class AziendaConfiguration : IEntityTypeConfiguration<Azienda>
{
    public  void Configure(EntityTypeBuilder<Azienda> builder)
    {
        builder
            .HasOne(azienda => azienda.ChildSoggetto)
            .WithOne(soggetto => soggetto.ParentAzienda)
            ;
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...