[英]entity framework 6 generate extra join when send query to sql
This is my framework that develop by entityframework 6: 这是我由entityframework 6开发的框架:
User DataModel : 用户数据模型:
[DataContract]
[Table("SEC_User")]
public class UserDataModel : EntityBase
{
[DataMember]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("UserID")]
public int UserID{get;set;}
[DataMember]
[Column("UserName")]
public string UserName{get;set;}
[DataMember]
[Column("UserKindRef")]
public int UserKindRef{get;set;}
.
.
.
}
[DataContract]
[Table("SEC_User_View")]
public class UserViewDataModel : UserDataModel
{
[DataMember]
[Column("UserKindTitle")]
public string UserKindTitle{get;set;}
}
DataAccess Method: 数据访问方法:
public UserViewDataModel GetByUserName(string userName)
{
using (Context)
{
return (from entity in Context.EntityList
where entity.UserName == userName
select entity).FirstOrDefault().ToEntity();
}
}
Sql Profiler : SQL事件探查器:
exec sp_executesql N'SELECT
[Limit1].[C1] AS [C1],
[Limit1].[UserID] AS [UserID],
[Limit1].[UserName] AS [UserName],
[Limit1].[UserKindRef] AS [UserKindRef],
[Limit1].[UserKindTitle] AS [UserKindTitle]
FROM ( SELECT TOP (1)
[Extent1].[UserKindTitle] AS [UserKindTitle],
[Extent2].[UserID] AS [UserID],
[Extent2].[UserName] AS [UserName],
[Extent2].[UserKindRef] AS [UserKindRef],
''0X0X'' AS [C1]
FROM [dbo].[SEC_User_View] AS [Extent1]
INNER JOIN [dbo].[SEC_User] AS [Extent2] ON [Extent1].[UserKey] =[Extent2].[UserKey]
WHERE ([Extent2].[UserName] = @p__linq__0) OR (([Extent2].[UserName] IS NULL) AND (@p__linq__0 IS NULL))
) AS [Limit1]',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'admin'
Unfortunately when UserViweDataModel Inherited from UserDataModel entityframework , entityframework use select to select and join SEC_User_View to SEC_User and it's wrong. 不幸的是,当UserViweDataModel继承自UserDataModelEntityframework时,entityframework使用select来选择SEC_User_View并将其与SEC_User联接,这是错误的。 It should be like this:
应该是这样的:
exec sp_executesql N'SELECT TOP (1)
[Extent1].[UserID] AS [UserID],
[Extent1].[UserName] AS [UserName],
[Extent1].[UserKindRef] AS [UserKindRef],
[Extent1].[UserKindTitle] AS [UserKindTitle]
FROM [dbo].[SEC_User_View] AS [Extent1]
WHERE ([Extent1].[UserName] = @p__linq__0) OR (([Extent1].[UserName] IS NULL) AND (@p__linq__0 IS NULL))',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'admin'
what's my mistake???? 我怎么了?
Table(SEC_User)
and Table(SEC_User_View)
. Table(SEC_User)
和Table(SEC_User_View)
。 You told EF you wanted two tables. 您告诉 EF您想要两张桌子。
Inheritance with EF: Table Per Hierarchy EF的继承:每个层次结构的表
The default for EF is TPH, but – by decorating both classes with Table
– you told it you want TPT (see part 2 of the blog article I posted above). EF的默认值为TPH,但是–通过用
Table
装饰两个类–您告诉它想要TPT(请参阅我上面发布的博客文章的第2部分)。
BTW, if you do want TPH – which it sounds like you do – the one and only table is going to be the base class, not the derived. 顺便说一句,如果您确实想要TPH(听起来像您一样),则唯一的表将是基类,而不是派生表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.