空合并运算符在Entity Framework 6中不起作用 [英] Null coalescing operator not working in Entity Framework 6
问题描述
我目前正在将应用程序从EF5迁移到EF6,但是运行该查询的单元测试遇到了问题:
I'm currently migrating an application from EF5 to EF6 but ran into an issue with a unit test that runs this query:
return (from employeeWorkLocation in Retrieve()
where employeeWorkLocation.ClientId == clientId
&& employeeWorkLocation.EmpUid == empUid
&& employeeWorkLocation.EffectiveDate <= effectiveDate
&& (!employeeWorkLocation.EffectiveEndDate.HasValue || employeeWorkLocation.EffectiveEndDate > effectiveDate)
join locationEntity in Context.WorkLocationEntities on employeeWorkLocation.WorkLocationUid equals locationEntity.WorkLocationUid into workLocations
from workLocation in workLocations.Where(wl => wl.Inactive == GenericYesNo.NO).DefaultIfEmpty()
select new EmployeeWorkLocation()
{
ClientId = employeeWorkLocation.ClientId,
EffectiveDate = employeeWorkLocation.EffectiveDate,
EffectiveEndDate = employeeWorkLocation.EffectiveEndDate,
EmployeeWorkLocationUid = employeeWorkLocation.EmployeeWorkLocationUid,
EmpUid = employeeWorkLocation.EmpUid,
MetaApplication = employeeWorkLocation.MetaApplication,
//MetaDateCreated = employeeWorkLocation.MetaDateCreated ?? DateTimeHelper.NowUnspecified,
MetaCreatedBy = employeeWorkLocation.MetaCreatedBy,
//MetaDateUpdated = employeeWorkLocation.MetaDateUpdated ?? DateTimeHelper.NowUnspecified,
MetaUpdatedBy = employeeWorkLocation.MetaUpdatedBy,
WorkLocationUid = employeeWorkLocation.WorkLocationUid,
HrLocationUid = workLocation.HRPLocationUid
}).OrderByDescending(e => e.EffectiveDate).FirstOrDefault();
由于某种原因,如果我删除上面的评论,则会出现此错误:
For some reason if I remove the comments above I get this error:
System.Data.Entity.Core.EntityCommandExecutionException:错误在执行命令定义时发生.见内在详细信息除外.---> System.ArgumentException:参数类型不匹配
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.ArgumentException: Argument types do not match
我尝试将这些行更改为长版本(三元运算符),但仍然没有运气.我收到相同的错误:
I tried changing those lines to the long version (ternary operator) but still no luck. I get same error:
MetaDateCreated = employeeWorkLocation.MetaDateCreated!=空吗?employeeWorkLocation.MetaDateCreated.Value:DateTimeHelper.NowUnspecified,
MetaDateCreated = employeeWorkLocation.MetaDateCreated != null ? employeeWorkLocation.MetaDateCreated.Value : DateTimeHelper.NowUnspecified,
employeeWorkLocation.MetaDateCreated
和 employeeWorkLocation.MetaDateUpdated
都是可空的 Datetime?
类型
DateTimeHelper.NowUnspecified
是 Datetime
不可为空的类型.与 MetaDateCreated
和 MetaDateUpdated
DateTimeHelper.NowUnspecified
is of Datetime
non-nullable type. Same as MetaDateCreated
and MetaDateUpdated
有什么想法吗?这在Entity Framework 5上运行良好
Any ideas? This was working fine with Entity Framework 5
更新:这是 DateTimeHelper.NowUnspecified
的定义:
public static DateTime NowUnspecified
{
get
{
return DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
}
}
如果我按照注释中的建议将 DateTimeHelper.NowUnspecified
替换为 DateTimeHelper.Now
,我的测试就会通过...
If I replace DateTimeHelper.NowUnspecified
with DateTimeHelper.Now
as suggested in the comments my test passes...
Update2 :使用LinqPad隔离问题后,我意识到Entity Framework 6在正确处理查询.问题出在Effort.EF6库,该库抛出异常
Update2: After isolating the issue using LinqPad I realized that Entity Framework 6 was handling the query correctly. The problem is with Effort.EF6 library which is throwing the exception
谢谢
推荐答案
问题是EF不知道如何将您的属性转换为sql.如果您确实需要使用该属性(或者将来会遇到类似情况),可以通过强制EF在逻辑的那一部分之前执行查询并将其应用于本地来实现:
The issue is that EF does not know how to translate your property to sql. If you really need to use that property (or run into situations like this in the future), you can do so by forcing EF to execute the query prior to that portion of the logic and apply it locally:
return (from employeeWorkLocation in Retrieve()
where employeeWorkLocation.ClientId == clientId
&& employeeWorkLocation.EmpUid == empUid
&& employeeWorkLocation.EffectiveDate <= effectiveDate
&& (!employeeWorkLocation.EffectiveEndDate.HasValue || employeeWorkLocation.EffectiveEndDate > effectiveDate)
join locationEntity in Context.WorkLocationEntities on employeeWorkLocation.WorkLocationUid equals locationEntity.WorkLocationUid into workLocations
from workLocation in workLocations.Where(wl => wl.Inactive == GenericYesNo.NO).DefaultIfEmpty()
select new{employeeWorkLocation, workLocation})
.ToArray() //this will cause EF to run the query
//Everything below this runs in the .NET code
//rather than on sql server
.Select(wl => new EmployeeWorkLocation()
{
ClientId = wl.employeeWorkLocation.ClientId,
EffectiveDate = wl.employeeWorkLocation.EffectiveDate,
EffectiveEndDate = wl.employeeWorkLocation.EffectiveEndDate,
EmployeeWorkLocationUid = wl.employeeWorkLocation.EmployeeWorkLocationUid,
EmpUid = wl.employeeWorkLocation.EmpUid,
MetaApplication = wl.employeeWorkLocation.MetaApplication,
MetaDateCreated = wl.employeeWorkLocation.MetaDateCreated ?? DateTimeHelper.NowUnspecified,
MetaCreatedBy = wl.employeeWorkLocation.MetaCreatedBy,
MetaDateUpdated = wl.employeeWorkLocation.MetaDateUpdated ?? DateTimeHelper.NowUnspecified,
MetaUpdatedBy = employeeWorkLocation.MetaUpdatedBy,
WorkLocationUid = wl.employeeWorkLocation.WorkLocationUid,
HrLocationUid = wl.workLocation?.HRPLocationUid
}).OrderByDescending(e => e.EffectiveDate).FirstOrDefault();
这篇关于空合并运算符在Entity Framework 6中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!