简体   繁体   English

实体框架InverseProperty注释用法

[英]Entity Framework InverseProperty annotation usage

if I have the following models, am I using the InverseProperty correctly? 如果我具有以下模型,是否可以正确使用InverseProperty?

class Person {
 public int PersonID {get;set;}

 [InverseProperty("Person")]
 public List<Hobbies> Sports {get;set;}

 [InverseProperty("Person")]
 public List<Hobbies> Art {get;set;}

 [InverseProperty("Person")]
 public List<Hobbies> Reading {get;set;}
}

abstract class Hobbies {
 public int HobbiesID {get;set;}
 public string HobbyName {get;set;}
 public int HobbyRating {get;set;}

 public int PersonID {get;set;}
 public Person Person {get;set;}
}

class Sports : Hobbies {}
class Art : Hobbies {}
class Reading : Hobbies {}

this works when I don't use the InverseProperty but the problem is that the database creates duplicate PersonID columns (like 4 of them, looks like 1 for each type that inherits from Hobbies), and I don't want that, 当我不使用InverseProperty时此方法有效,但问题是数据库创建了重复的PersonID columns (像其中的4个,对于从Hobbies继承的每种类型看起来像1个),我不希望这样,

However, when I use InversePorperty I get an exception: 但是,当我使用InversePorperty ,会出现异常:

Schema specified is not valid. Errors: The relationship 'x.x.Person_Reading' was not loaded because the type 'x.x.Hobbies' is not available.

First, This looks strange : you are creating multiple mapping to the same object qwith different names ? 首先,这看起来很奇怪:您正在创建到具有不同名称的同一对象q的多个映射?

 [InverseProperty("Person")]
 public List<Hobbies> Sports {get;set;}

 [InverseProperty("Person")]
 public List<Hobbies> Art {get;set;}

 [InverseProperty("Person")]
 public List<Hobbies> Reading {get;set;}

It should be something like : 应该是这样的:

[InverseProperty("Person")]
public virtual List<Hobbies> Hobbies {get;set;}

[NotMapped]
public List<Sport> Sports 
{
    get
    {
        return this.Hobbies.OfType<Sport>().ToList();
    }
}


[NotMapped]
public List<Art> Art 
{
    get
    {
        return this.Hobbies.OfType<Art>().ToList();
    }
}


[NotMapped]
public List<Reading> Readings 
{
    get
    {
        return this.Hobbies.OfType<Reading>().ToList();
    }
}

If you set the property Person in the abstract class, then the mapping must be to the abstract class. 如果在抽象类中设置属性Person,则映射必须是到抽象类的。

Otherwise, you have to declare the PersonId in the abstract class and then set the Person property in every concrete class using the attribute [ForeignKey("PersonId")]. 否则,您必须在抽象类中声明PersonId,然后使用属性[ForeignKey(“ PersonId”)]在每个具体类中设置Person属性。 But this solution is pretty strange. 但是这种解决方案很奇怪。

Secondly, if you want to specify the ForeignKey for the Person, you should use : 其次,如果要为Person指定ForeignKey,则应使用:

[ForeignKey("PersonID")]
public virtual Person Person {get;set;}

Third : Are you sure you don't need MN relation? 第三:确定不需要MN关系吗? Do you really want every people to create new Hobbies (you would finally have multiple times "Driving" (or whatever) as a hobby). 您是否真的要每个人都创建新的兴趣爱好(您最终将多次“驾驶”(或其他)作为兴趣爱好)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM