EF Core 5:使用与 FK 相同的 PK 并且没有父属性来配置拥有的类型 [英] EF Core 5: Configuring owned type with PK same as FK and with no parent property
本文介绍了EF Core 5:使用与 FK 相同的 PK 并且没有父属性来配置拥有的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试迁移到 EF Core 5.以前一切正常.我有以下结构,我不想更改.
I'm trying to migrate to EF Core 5. Everything used to work before. I have the following structure that I don't want to change.
ActionSetup.cs
ActionSetup.cs
public class ActionSetup
{
public Conditions Conditions { get; set; }
}
public class Conditions
{
public string SomeValue { get; set; }
}
EntityConfiguration.cs
EntityConfiguration.cs
public class EntityConfiguration: IEntityTypeConfiguration<ActionSetup>
{
builder.OwnsOne(tah => tah.Conditions, a =>
{
a.ToTable("action_conditions");
a.Property<long>("id");
a.HasKey("id");
a.Property(p => p.SomeValue ).HasColumnName("some_value");
});
}
但是现在,当我尝试获取 ActionSetup 时,出现错误,因为 EF Core 认为我的 FK 被称为 ActionSetupId
.我不确定以前它是如何工作的,但是有没有办法让它工作,但是:
But now, when I try to fetch an ActionSetup, I get an error because EF Core thinks my FK is called ActionSetupId
. I am not sure how this used to work before but is there a way to make this work, but:
- 无需添加单独的 FK.
- 没有在
Conditions
类中添加属性?
- Without adding a separate FK.
- Without adding a property inside the
Conditions
class?
推荐答案
只需配置ActionSetupId"即可映射到id"的阴影属性在表中.
Just configure the "ActionSetupId" shadow property to map to "id" in the table.
modelBuilder.Entity<ActionSetup>()
.OwnsOne(tah => tah.Conditions, a =>
{
a.ToTable("action_conditions");
a.Property<long>("ActionSetupId").HasColumnName("id");
a.Property(p => p.SomeValue).HasColumnName("some_value");
});
创造
CREATE TABLE [action_conditions] (
[id] bigint NOT NULL,
[some_value] nvarchar(max) NULL,
CONSTRAINT [PK_action_conditions] PRIMARY KEY ([id]),
CONSTRAINT [FK_action_conditions_ActionSetup_id] FOREIGN KEY ([id]) REFERENCES [ActionSetup] ([Id]) ON DELETE CASCADE
);
这篇关于EF Core 5:使用与 FK 相同的 PK 并且没有父属性来配置拥有的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文