同一类之间的实体框架关系 [英] Entity Framework relation between the same classes
问题描述
我有一个 Location
类,并且我希望该类具有对另外两个 Location
对象的引用:PreviousLocation和NextLocation.我已经尝试过了:
公共类位置{public int LocationId {get;放;}[ForeignKey("LocationId")]public Location PreviousLocation {get;放;}[ForeignKey("LocationId")]public Location NextLocation {获取;放;}}
但是在运行 Add-Migration
命令时给了我一个错误:实体类型'Location'中有多个导航指向相同的属性集-使用ForeignKeyAttribute的'LocationId'.>
有人可以给我看一个如何实现我想要的行为的例子吗?
尝试一下.这将允许递归或分层引用.
公共类位置{public int LocationId {get;放;}public int NextLocationId {get;放;}public int PreviousLocationId {get;放;}[ForeignKey("PreviousLocationId")]public Location PreviousLocation {get;放;}[ForeignKey("NextLocationId")]public Location NextLocation {获取;放;}}
I have a Location
class, and I want that class to have references to another two Location
objects: PreviousLocation and NextLocation. I have tried this:
public class Location
{
public int LocationId { get; set; }
[ForeignKey("LocationId")]
public Location PreviousLocation { get; set; }
[ForeignKey("LocationId")]
public Location NextLocation { get; set; }
}
But it gave me an error when running Add-Migration
command: There are multiple navigations in entity type 'Location' which are pointing to same set of properties - 'LocationId' using ForeignKeyAttribute.
Could anybody show me an example of how to achieve my desired behavior?
Try this. This will allow Recursive or hierarchical referencing.
public class Location
{
public int LocationId { get; set; }
public int NextLocationId { get; set; }
public int PreviousLocationId { get; set; }
[ForeignKey("PreviousLocationId")]
public Location PreviousLocation { get; set; }
[ForeignKey("NextLocationId")]
public Location NextLocation { get; set; }
}
这篇关于同一类之间的实体框架关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!