【发布时间】:2020-08-09 18:58:38
【问题描述】:
我正在尝试建立这样的关系:
public class MyContainer {
public int Id {get;set;}
}
public class MyCompontentOne
{
public MyContainer MyContainer {get;set;}
public string Prop1{get;set;}
}
我想把它配置成这样的表格:
MyContainerId (FK & PK) | Prop1 |... other props
我使用 Fluent API 尝试了很多东西,但无论我做什么,我总是会遇到一些错误。这是我尝试过的最新方法:(添加影子属性并使其成为外键)
mb.Entity<MyCompontentOne>().Property<int>("MyContainerId").IsRequired();
mb.Entity<MyCompontentOne>().Property(o => o.Prop1).IsRequired();
mb.Entity<MyCompontentOne>()
.HasOne(o => o.MyContainer)
.WithOne();
mb.Entity<MyCompontentOne>().HasKey("MyContainerId");
【问题讨论】:
-
这能回答你的问题吗? EF Core One to One or Zero Relationship
-
嗯,您在我写答案时更新了问题。您的最新更新有什么问题,基本上它与我的答案相同(只是使用更明确的属性映射)?我在发布之前测试了我的答案,并且没有错误地获得了所需的数据库模型。
标签: entity-framework-core ef-core-3.1