EF 5.0 table-per-type继承提供了奇怪的查询

时间:2013-01-02 10:07:12

标签: c#-4.0 ef-code-first entity-framework-4.1 entity-framework-5 table-per-type

我在代码第一种方法中对现有模式有标准的每表类型继承情况:

[Table("base_object")]
public partial class BaseObject
{
  public BaseObject()
  { 

  }

  [Key]
  [Column("id")]
  public long Id {get; set;}
  [Column("slug")]
  public string Slug {get; set;}
  ...       
}

[Table("user")]
public partial class User: BaseObject
{
  public User()
  {

  }

  [Column("login")]
  public string Login {get; set;}
  [Column("email")]
  public string Email {get; set;}
}

如注释中所述,映射很简单,user.id是base_object.id的主键和外键

在测试用例中,在第一次从Context调用“Users”存储库时,EF正在生成查询:

SELECT 
1 AS [C1], 
'0X0X' AS [C2], 
[Extent1].[id] AS [id], 
[Extent2].[slug] AS [slug], 
[Extent1].[login] AS [login], 
[Extent1].[email] AS [email], 
[Extent1].[BaseObject_id] AS [BaseObject_id]
FROM  [dbo].[user] AS [Extent1]
INNER JOIN [dbo].[base_object] AS [Extent2] ON [Extent1].[id] = [Extent2].[id]

这会产生错误,因为列“user.BaseObject_id”不存在! Whitout这一行:* [Extent1]。[BaseObject_id] AS [BaseObject_id] *查询没问题。

我做错了什么?

我正在使用带有.NET 4.0的EF5.0

1 个答案:

答案 0 :(得分:0)

原来,“......”产生了不同。

BaseObject有一个集合:

[InverseProperty("Object")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights { get; set; }

User有一个集合:

[InverseProperty("User")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights { get; set; }

这导致奇怪的查询生成。我必须从基础对象类中删除重复的集合,它开始工作。