我有以下linq查询(false和true是因为动态创建查询):
(from x in ((ObjectQuery<PersonView>)PersonView).MergeAs(0)
where x.Info && (((False || (True && (x.Person.FirstName == compareValue))) || (True && (x.Person.FirstName == compareValue))) || (True && ((x.Person.FirstName == "") || (x.Person.FirstName == null))))
where (x.DateTime >= startDate.Date) && (x.DateTime <= endDate.Date)
select x.PersonID).Distinct()
它转换为这个SQL查询:
SELECT
[Distinct1].[PersonID] AS [PersonID]
FROM ( SELECT DISTINCT
[Extent1].[PersonID] AS [PersonID]
FROM (SELECT
[PersonView].[PersonID] AS [PersonID],
[PersonView].[DateTime] AS [DateTime],
[PersonView].[Info] AS [Info]
FROM [core].[PersonView] AS [PersonView]) AS [Extent1]
LEFT OUTER JOIN [core].[Persons] AS [Extent2] ON [Extent1].[PersonID] = [Extent2].[PersonID]
WHERE ([Extent1].[Info] = 1) AND ([Extent2].[FirstName] IN (@p__linq__0,@p__linq__1,N'') OR [Extent2].[FirstName] IS NULL) AND ([Extent1].[DateTime] >= @p__linq__2) AND ([Extent1].[DateTime] <= @p__linq__3)
) AS [Distinct1]
我的目标是因为性能而将内部联接替换为左联接。我一直在搜索,我已经读过,如果该属性不可为空(在这种情况下的人不可为空),它应该被转换为内连接。这显然不是这种情况。如何让Entity Framework生成内部联接?
编辑:Here说明必需的关联应转换为内部联接。什么使它成为必需的关联?在我的情况下,外键不可为空。
我先工作数据库。这就是生成的内容:
public partial class PersonView: DbEntity
{
private int _personID;
private System.DateTime _dateTime;
private bool _info;
public int PersonID { get{ return _personID;} set{ _personID = value; OnPropertyChanged("PersonID");} }
public System.DateTime DateTime { get{ return _dateTime;} set{ _dateTime = value; OnPropertyChanged("DateTime");} }
public bool Info{ get{ return _info;} set{ _info = value; OnPropertyChanged("Info");} }
public virtual Person Person { get; set; }
}
我尝试添加元数据,但没有区别。
[MetadataType(typeof(PersonViewMetaData))]
public partial class PersonView
{ }
public sealed class PersonViewMetaData
{
[Required]
public Person Person { get; set; }
}