C# 如何在EF Core中配置、映射一对一关系
作为一名学生,我必须使用.NETCore设计我的第一个WebApi。我已经搜索了两天,还没有找到解决我问题的答案。也许因为我是初学者 我有以下课程:C# 如何在EF Core中配置、映射一对一关系,c#,.net-core,entity-framework-core,ef-fluent-api,C#,.net Core,Entity Framework Core,Ef Fluent Api,作为一名学生,我必须使用.NETCore设计我的第一个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 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; }
}
我想配置我的数据库,这样我就可以得到所有船只的列表,以及它们的位置。但我还要一份所有地点的清单,如果有船,是或否
在我看来,这两个属性都应该是可选的。一个地点不一定拥有一艘船,一艘船也不一定拥有一个地点
我试过:
…但没有结果
编辑:
我想有一个DbSet船,显示所有我的船与他们的位置。还有一个显示所有位置及其船只的数据库集位置。试试这个:
public class Boat
{
public int Id { get; set; }
public int BoatNr { get; set; }
public string BoatName { get; set; }
public int? LocationId { get; set; }
public virtual Location Location {get; set;}
}
public class Location
{
public int Id{ get; set; }
public int Row { get; set; }
public char Side { get; set; }
public int Level { get; set; }
public virtual ICollection<Boat> Boats { get; set; }
//you can use this instead of collection
public virtual Boat Boat { get; set; }
//but I don't recommend to do this
}
公共级船
{
公共int Id{get;set;}
public int boatrn{get;set;}
公共字符串BoatName{get;set;}
public int?LocationId{get;set;}
公共虚拟位置{get;set;}
}
公共类位置
{
公共int Id{get;set;}
公共int行{get;set;}
公共字符侧{get;set;}
公共整数级别{get;set;}
公共虚拟ICollection{get;set;}
//你可以用这个代替集合
公共虚拟船{get;set;}
//但我不建议这样做
}
由于您使用的是NetCore5,所以不需要任何流畅的API,那么这段代码做什么呢?您能否显示您的配置和访问数据库的代码,并解释您期望发生的事情和实际发生的事情?对不起,我输入了一个错误,存储应该是位置。