且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架InverseProperty注解的使用

更新时间:2023-11-22 13:11:52

首先,
这看起来很奇怪:你正在创建多个映射到同一个对象qwith不同的名字

  [InverseProperty(人)]
 公开名单<爱好>体育{获取;设置;} [InverseProperty(人)]
 公开名单<爱好>艺术{获取;设置;} [InverseProperty(人)]
 公开名单<爱好>阅读{获取;集;}

应该是这样的:

  [InverseProperty(人)]
公共虚拟目录<爱好>嗜好{获取;设置;}[NotMapped]
公开名单<体育GT;体育
{
    得到
    {
        返回this.Hobbies.OfType<运动方式>()了ToList();
    }
}
[NotMapped]
公开名单<艺术与GT;艺术
{
    得到
    {
        返回this.Hobbies.OfType<艺术方式>()了ToList();
    }
}
[NotMapped]
公开名单<阅读>阅读
{
    得到
    {
        返回this.Hobbies.OfType<阅读方式>()了ToList();
    }
}

如果你设置了抽象类的财产的人,则映射必须是抽象类。

否则,您必须声明PERSONID在抽象类,然后使用属性[ForeignKey的(PERSONID)]在每一个具体类设置个人财产。但这种解决方案是pretty奇怪的。

其次,如果你想指定的人ForeignKey的,你应该使用:

  [ForeignKey的(是PersonID)]
公共虚拟人人{获取;集;}

第三:你确定你不需要M-N的关系?你真的希望每个人创造新的嗜好(你最终将有多次驾驶(或其他)作为一种业余爱好)。

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