【发布时间】:2021-03-09 12:56:34
【问题描述】:
作为一名学生,我必须使用 .Net Core 设计我的第一个 WebApi。我一直在寻找两天,并没有找到解决我问题的答案。也许是因为我是初学者。
我有以下课程:
public class Boat
{
public int BoatId { get; set; }
public int BoatNr { get; set; }
public string BoatName { get; set; }
public Location Location { get; set; }
}
public class Location
{
public int LocationId { get; set; }
public int Row { get; set; }
public char Side { get; set; }
public int Level { get; set; }
public Boat Boat { get; set; }
}
我想配置我的数据库,以便获得所有船只的列表及其位置。但我也想要一份所有地点的清单,如果有船,是或否。
在我看来,这两个属性都应该是可选的。位置不一定拥有船,船也不一定有位置。
我试过了:
Entity Framework Core zero-or-one to zero-or-one relation
Implementing Zero Or One to Zero Or One relationship in EF Code first by Fluent API
...但没有结果
编辑: 我想要一个 DbSet Boats 来显示我所有的船及其位置。还有一个 DbSet Locations,显示所有位置及其船只。
【问题讨论】:
-
那么这段代码有什么作用?您能否展示您的配置和访问数据库的代码,并解释您期望发生什么以及实际发生什么?
标签: c# .net-core entity-framework-core ef-fluent-api