的EntityFramework。流利的Api。反转映射

时间:2016-08-11 09:49:21

标签: c# entity-framework ef-fluent-api

我有以下课程:

class Parent
{
    public int ID;
    public Child Child1;
    public Child Child2;
}

class Child
{
    public int ID;
    public string Data;
}

使用DbModelBuilder我有以下Fluent API代码:

EntityTypeConfiguration<Parent> adrInEgrul 
    = modelBuilder.Entity<Parent>();
adrInEgrul.HasKey(x => x.ID);
adrInEgrul.HasOptional(x => x.Child1)
    .WithOptionalPrincipal();
adrInEgrul.HasOptional(x => x.Child2)
    .WithOptionalPrincipal();

所有这些给了我以下表格:
enter image description here

但我确实需要以下内容:
enter image description here

我很高兴得到我想要的只是修复Fluent API部分,但不知道怎么做(如果它可能的话)。
将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

在callum-linington评论之后,我想出了以下Fluent API代码,它完成了这项工作。

EntityTypeConfiguration<Parent> adrInEgrul 
    = modelBuilder.Entity<Parent>();
adrInEgrul.HasKey(x => x.ID);
adrInEgrul.HasOptional(x => x.Child1);
adrInEgrul.HasOptional(x => x.Child2);
相关问题