实体框架6 - 基类上的继承和导航属性 [英] Entity Framework 6 - inheritance and navigation properties on base class

查看:121
本文介绍了实体框架6 - 基类上的继承和导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有导航属性和继承问题。

I have a problem with navigation properties and inheritance.

这是我的问题:
我有一个基础 Person 类和类用户 Worker 继承自 Person 。在DB级别,我使用单表继承或每层次表(TPH)继承。所以有一个带有歧视列的表。

This is my problem: I have a base Person class and classes User and Worker which inherit from Person. On the DB level I'm using single table inheritance or table per hierarchy (TPH) inheritance. So there a single table with a discriminator column.

用户 Worker 需要有一个公司关系,所以我想在 Person 类中定义它。

Both User and Worker need to have a Company relation, so I would like to define it on the Person class.

我定义了我的模型:

[Table("mydb.person")]
public abstract partial class Person
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long ID { get; set; }

    public long? CompanyID { get; set; }
    [ForeignKey("CompanyID")]
    public virtual Company Company { get; set; }
    ...
}

public partial class User : Person
{
    ...
}

public partial class Worker : Person
{
    ....
}

[Table("mydb.company")]
public partial class Company
{
    public Company()
    {
        this.People = new HashSet<Person>();
        this.Users = new HashSet<User>();
        this.Workers = new HashSet<Worker>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long ID { get; set; }

    public virtual ICollection<Person> People { get; set; }

    public virtual ICollection<AgriviUser> Users { get; set; }

    public virtual ICollection<Worker> Workers { get; set; }

    ...
}

现在,当我尝试查询以获取用户和相关公司,例如:

Now, when I try to do a query to get the user and related company, for example:

dbSet.Where(u => u.Username == username).Include(x => x.Company).FirstOrDefault();

查询失败,此例外:


未知列'字段列表中的'Extent1.Company_ID'

Unknown column 'Extent1.Company_ID' in 'field list

如果我检查结果SQL,这样做:

If I examine the result SQL it looks something like this:

SELECT
1 AS `C1`, 
@gp2 AS `C2`, 
`Extent1`.`ID`, 
`Extent1`.`CompanyID`, 
`Extent1`.`Username`,
...
`Extent1`.`Company_ID`
FROM `person` AS `Extent1`
 WHERE `Extent1`.`Discriminator` = @gp1 

它包含额外的 Company_ID 列,不存在。

It includes the extra Company_ID column, which doesn't exist.

我尝试了一些事情,没有任何工作:

I tried a few thing, nothing worked out:


  • CompanyID Company_ID - >它在SQL中生成一个 Column_ID1 并抛出相同的异常

  • 公司中删除​​用户工作者 - >它抛出一个例外,它不知道如何映射用户公司实体:

  • renaming the column from CompanyID to Company_ID -> it generates a Column_ID1 in SQL and throws the same exception
  • removing the Users and Workers relations from Company -> it throws an exception saying it doesn't know how to map User and Company entities:

无法确定
类型Models.User和Models.Company之间的关联的主体结束。必须使用
关系流畅的API或数据注释来明确配置此
关联的主体。

Unable to determine the principal end of an association between the types 'Models.User' and 'Models.Company'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.




  • 如果我从公司中删除​​所有3个导航属性,它将抛出与上述相同的映射异常

    • If I remove all 3 navigation properties from Company it throws the same mapping exception as above
    • 我现在不在干净的想法。唯一可以工作的是做一些肮脏的黑客,定义孩子类的所有关系,并且在需要用户和工作者的情况下,在基类中执行单独的查询和合并。

      I'm out of "clean" ideas at the moment. The only thing that could work is to do some dirty hack, define all the relations on child classes, and do separate queries and merging in the base class if both users and workers are required.

      你有什么建议吗?

      推荐答案

      删除用户和工作者的收藏属性。

      Remove the Users and Workers collection properties.

      public virtual ICollection<User> Users { get; set; }
      
      public virtual ICollection<Worker> Workers { get; set; }
      

      由于您的公司导航属性是在Person上定义的,所以关联的后导航属性必须是ICollection人。

      As your Company navigation property is defined on Person the associated back navigation property has to be an ICollection of Person.

      People集合将包含所有关联的工作人员和用户。两个额外的属性用户和工作人员被解释为全新的关系,并且因为您没有相应的属性,用户或工作人员EF的外键会虚拟地生成。

      The People collection will contain all the associated workers and users. The two extra properties Users and Workers are interpreted as completely new relationships and because you do not have corresponding properties and foreign keys on User or Worker EF generates it virtually.

      这篇关于实体框架6 - 基类上的继承和导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆