我在代码第一种方法中对现有模式有标准的每表类型继承情况:
[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
答案 0 :(得分:0)
原来,“......”产生了不同。
BaseObject
有一个集合:
[InverseProperty("Object")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights { get; set; }
User
有一个集合:
[InverseProperty("User")]
public virtual ICollection<UserHasObjectRight> UserHasObjectRights { get; set; }
这导致奇怪的查询生成。我必须从基础对象类中删除重复的集合,它开始工作。