EF Core 5:配置 PK 与 FK 相同但没有父级的拥有类型 属性
EF Core 5: Configuring owned type with PK same as FK and with no parent property
我正在尝试迁移到 EF Core 5。以前的一切都可以正常工作。我有以下我不想更改的结构。
ActionSetup.cs
public class ActionSetup
{
public Conditions Conditions { get; set; }
}
public class Conditions
{
public string SomeValue { get; set; }
}
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
。我不确定它以前是如何工作的,但有没有办法让它工作,但是:
- 不添加单独的 FK。
- 没有在
Conditions
里面加一个属性 class?
只需配置“ActionSetupId”阴影 属性 以映射到 table 中的“id”。
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。以前的一切都可以正常工作。我有以下我不想更改的结构。
ActionSetup.cs
public class ActionSetup
{
public Conditions Conditions { get; set; }
}
public class Conditions
{
public string SomeValue { get; set; }
}
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
。我不确定它以前是如何工作的,但有没有办法让它工作,但是:
- 不添加单独的 FK。
- 没有在
Conditions
里面加一个属性 class?
只需配置“ActionSetupId”阴影 属性 以映射到 table 中的“id”。
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
);