我有两个需要建立关系的实体。我已经像这样设置了模型以及我的
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);
按如下方式配置您的模型:
modelBuilder.Entity<Result>()
.HasOne(i => i.Fixture)
.WithOne(a => a.Result)
.HasForeignKey<Fixture>(c => c.Id);