Entity Framework InverseProperty annotation usage

2020-05-01 08:45发布

if I have the following models, am I using the InverseProperty correctly?

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,

However, when I use InversePorperty I get an exception:

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

1条回答
可以哭但决不认输i
2楼-- · 2020-05-01 09:01

First, This looks strange : you are creating multiple mapping to the same object qwith different names ?

 [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.

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")]. But this solution is pretty strange.

Secondly, if you want to specify the ForeignKey for the Person, you should use :

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

Third : Are you sure you don't need M-N relation? Do you really want every people to create new Hobbies (you would finally have multiple times "Driving" (or whatever) as a hobby).

查看更多
登录 后发表回答