简体   繁体   中英

Entity Framework InverseProperty annotation usage

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.

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 MN relation? Do you really want every people to create new Hobbies (you would finally have multiple times "Driving" (or whatever) as a hobby).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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