我目前正在将应用程序从EF5迁移到EF6,但是在运行此查询的单元测试中遇到了问题:

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();


由于某种原因,如果我删除上面的评论,则会出现此错误:


  System.Data.Entity.Core.EntityCommandExecutionException:错误
  在执行命令定义时发生。见内在
  详情除外。 ---> System.ArgumentException:参数类型
  不符合


我尝试将这些行更改为长版本(三元运算符),但仍然没有运气。我得到同样的错误:


  MetaDateCreated = employeeWorkLocation.MetaDateCreated!= null吗?
  employeeWorkLocation.MetaDateCreated.Value:
  DateTimeHelper.NowUnspecified,


employeeWorkLocation.MetaDateCreatedemployeeWorkLocation.MetaDateUpdated都是可为空的Datetime?类型

DateTimeHelper.NowUnspecifiedDatetime不可为空的类型。与MetaDateCreatedMetaDateUpdated相同

有任何想法吗?这在Entity Framework 5上运行良好

更新:这是DateTimeHelper.NowUnspecified的定义:

public static DateTime NowUnspecified
{
    get
    {
        return DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
    }
}


如果按照注释中的建议将DateTimeHelper.NowUnspecified替换为DateTimeHelper.Now,则我的测试通过...

Update2:使用LinqPad隔离问题后,我意识到Entity Framework 6在正确处理查询。问题出在Effort.EF6库中,该库抛出异常

谢谢,

最佳答案

问题是EF不知道如何将您的属性转换为sql。如果您确实需要使用该属性(或将来会遇到类似情况),可以通过强制EF在逻辑的该部分之前执行查询并将其应用于本地来实现:

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();

10-06 00:44