是否可以在同一个命名属性上使用 EF4.2 Code First 和 Inverse Property Attribute?
我想知道是否有一种方法可以实现同一类上同一实体的相关列表,或者使用迁移来设置具有此属性的模型?
public class Person
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
[InverseProperty("FamilyMembers")]
public List<Person> FamilyMembers { get; set; }
}
目前,当我使用迁移(AddMigration)来设置数据库时,出现以下异常:
添加迁移:无法从属性建立关系 “ConsoleApplication3.Person”类型上的“FamilyMembers”属性 “ConsoleApplication3.Person”类型上的“FamilyMembers”。检查值 在 InversePropertyAttribute 中以确保关系定义 是唯一的,并且从一个导航属性到其 相应的反向导航属性。
另外,在运行测试应用程序时,将人员添加到人员数据库集中时出现相同的错误。
var p1 = new Person();
p1.Id = Guid.NewGuid();
p1.Name = "p1";
var p2 = new Person();
p2.Id = Guid.NewGuid();
p2.Name = "p2";
var c = new TestContext();
c.People.Add(p1);
是否存在另一个不唯一且引用相同导航属性的属性?
I want to know if there is a way to achieve a related list of the same entity on the same class or use migrations to setup a model with this attribute in it?
public class Person
{
[Key]
public Guid Id { get; set; }
public string Name { get; set; }
[InverseProperty("FamilyMembers")]
public List<Person> FamilyMembers { get; set; }
}
At the moment when I am using migrations (AddMigration) to setup the database I get the following exception:
Add-Migration : A relationship cannot be established from property
'FamilyMembers' on type 'ConsoleApplication3.Person' to property
'FamilyMembers' on type 'ConsoleApplication3.Person'. Check the values
in the InversePropertyAttribute to ensure relationship definitions
are unique and reference from one navigation property to its
corresponding inverse navigation property.
Also I get the same error adding Person's to the People dbset when running the a test app.
var p1 = new Person();
p1.Id = Guid.NewGuid();
p1.Name = "p1";
var p2 = new Person();
p2.Id = Guid.NewGuid();
p2.Name = "p2";
var c = new TestContext();
c.People.Add(p1);
Is there another attribute that is not unique and references the same navigation property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的答案是否定的。关系的每一端都必须有自己的导航属性。
The simple answer is no. Each end of the relation must have its own navigation property.