实体框架 InverseProperty 注释使用

Entity Framework InverseProperty annotation usage(实体框架 InverseProperty 注释使用)
本文介绍了实体框架 InverseProperty 注释使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下模型,我是否正确使用了 InverseProperty?

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 {}

这在我不使用 InverseProperty 时有效,但问题是数据库创建了重复的 PersonID 列(其中 4 个,每种类型看起来像 1 个继承自爱好),我不希望这样,

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,

但是,当我使用 InversePorperty 时出现异常:

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.

推荐答案

首先,这看起来很奇怪:您正在创建多个映射到具有不同名称的同一个对象 q?

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;}

应该是这样的:

[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();
    }
}

如果你在抽象类中设置了Person属性,那么映射必须是抽象类.

If you set the property Person in the abstract class, then the mapping must be to the abstract class.

否则,您必须在抽象类中声明 PersonId,然后使用属性 [ForeignKey("PersonId")] 在每个具体类中设置 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")]. But this solution is pretty strange.

其次,如果你想为 Person 指定 ForeignKey,你应该使用:

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

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

第三:你确定你不需要 M-N 关系吗?你真的希望每个人都创造新的爱好吗(你最终会有多次驾驶"(或其他)作为爱好).

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

这篇关于实体框架 InverseProperty 注释使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)