[英]EF4 Code First: Parent/Child relationship in same table, how to get inverse property?
I have a class that gets arranged into a hierarchy, such as this:我有一个 class 排列成一个层次结构,例如:
class Item
{
[Key]
public int ID {get;set;}
public string PropA {get;set;}
public string PropB {get;set;}
public Virtual ICollection<Item> Children {get;set;}
}
This works great, and EF creates a Item_ID key to identify the parent.这很好用,并且 EF 创建了一个 Item_ID 键来标识父级。 I am exporting this data via my MVC app and need to access this key however.
我正在通过我的 MVC 应用程序导出这些数据,但是需要访问这个密钥。
How can I access the Parent ID, from an Item?如何从项目访问父 ID?
I tried [InverseProperty] tag, but it seemed like it wanted to create yet another key -- How can I get it to use the existing Item_ID field that is already in my database and working properly with my Children navigation property?我尝试了 [InverseProperty] 标记,但它似乎想创建另一个键——我怎样才能让它使用我的数据库中已经存在的现有 Item_ID 字段并与我的 Children 导航属性一起正常工作?
Thanks!谢谢!
Okay, I had to change the name of my database column frmo Item_ID to Parent_ID manually, then everything worked.好的,我必须手动将我的数据库列 frmo Item_ID 的名称更改为 Parent_ID,然后一切正常。 I have the following:
我有以下内容:
[InverseProperty("Parent")]
public virtual ICollection<Item> Children { get; set; }
[InverseProperty("Contents")]
public virtual Item Parent { get; set; }
I think you would have to add a我想你必须添加一个
public virtual Item Parent {get;set;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.