实体框架中的一对一关系

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

我有两个需要建立关系的实体。我已经像这样设置了模型以及我的

ApplicationDbContext
。但是,当我尝试对其运行查询以从相关实体获取数据时,我收到此错误。

无法确定“Fixture.Result”和“Result.Fixture”之间的一对一关系的依赖方。要识别关系的依赖方,请配置外键属性

我怎样才能让它发挥作用?

我需要

Fixture
可选有一个
Result
,但是
Result
必须有一个
Fixture

夹具.cs

public class Fixture
{
    public long Id { get; set; }
    public DateTime Date { get; set; }
    public League League { get; set; }
    public Season Season { get; set; }
    public Result Result { get; set; }
    //Properties omitted for brevity
}

结果.cs

public class Result
{
    public long Id { get; set; }
    public League League { get; set; }
    public SeasonDivision Division { get; set; }
    public SeasonCalendarDate SeasonCalendarDate { get; set; }
    public Fixture Fixture { get; set; }
    //Properties omotted for brevity
}

ApplicationDbContext.cs

builder.Entity<Result>()
    .HasOne(i => i.Fixture); 
c# .net entity-framework
1个回答
0
投票

按如下方式配置您的模型:

  modelBuilder.Entity<Result>()
              .HasOne(i => i.Fixture)
              .WithOne(a => a.Result)
              .HasForeignKey<Fixture>(c => c.Id);
© www.soinside.com 2019 - 2024. All rights reserved.