EF 5.0 table-per-type 继承给出了奇怪的查询
Posted
技术标签:
【中文标题】EF 5.0 table-per-type 继承给出了奇怪的查询【英文标题】:EF 5.0 table-per-type inheritance gives strange queries 【发布时间】:2013-01-02 10:07:11 【问题描述】:在现有模式的代码优先方法中,我有标准的 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 首次调用“用户”存储库期间,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”不存在! 没有这一行:*[Extent1].[BaseObject_id] AS [BaseObject_id]* 查询就可以了。
我做错了什么?
我将 EF5.0 与 .NET 4.0 一起使用
【问题讨论】:
【参考方案1】:事实证明,“...”产生了影响。
BaseObject
有一个收藏:
[InverseProperty("Object")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights get; set;
而User
有一个收藏:
[InverseProperty("User")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights get; set;
它导致了奇怪的查询生成。我必须从基对象类中删除重复的集合,它开始工作了。
【讨论】:
以上是关于EF 5.0 table-per-type 继承给出了奇怪的查询的主要内容,如果未能解决你的问题,请参考以下文章