如果首先使用EF核心代码不存在映射,则会发生错误

问题描述 投票:0回答:1

大家好,我在下面有这样的模型sections

public class Sections
{
    public int SectionId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public  Requests Requests { get; set; }
}

以下各节模型的数据结构如下

 sectionId      Name      description
      1         code1       code 1
      2         code2       code 2

我还有一个模型Requests,并且该模型如下所示

public class Requests
{
    public int RequestId { get; set; }
    public string  Description { get; set; }
    public int SectionId { get; set; }
    public  Sections sections { get; set; }
}

以及如下所示的Requests模型的示例数据的结构

RequestId   Description   SectionId 
    1          test1         1
    2          test2         1
    3          test1         2
    4          test2         2

使用模型数据的这种结构,我正在下面映射这两个模型

  modelBuilder.Entity<Requests>()
     .HasOne(a => a.sections)
     .WithOne(o => o.Requests); //not sure if this is correct way to map these two models with one-to-many mapping as listed in requests model

是上述映射是实现相同目标的正确方法,并且我正在使用Entity Framework核心代码优先方法。

如果我不使用上面的映射,则会出现此错误:

无法确定Requests.sectionsSections.Requests之间的一对一关系的子/依存侧。

任何人都可以让我知道是否有其他方法可以映射这两个模型

c# entity-framework entity-framework-core ef-code-first ef-code-first-mapping
1个回答
0
投票

尝试一下

  modelBuilder.Entity<sections>()
 .HasOne(a => a.Requests)
© www.soinside.com 2019 - 2024. All rights reserved.