2016-08-13 47 views
2

我目前正在遷移從EF5申請EF6但跑進了單元測試,運行此查詢的問題:如果我刪除空合併運算符實體框架中不靈6

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: An error occurred while executing the command definition. See the inner exception for details. ---> System.ArgumentException: Argument types do not match

我試圖改變這些線的長版(三元運算符),但仍然沒有運氣。我得到同樣的錯誤:

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

employeeWorkLocation.MetaDateCreatedemployeeWorkLocation.MetaDateUpdated都是可空Datetime?

DateTimeHelper.NowUnspecified的是Datetime非空類型。相同MetaDateCreatedMetaDateUpdated

任何想法?這是工作的罰款與實體框架5

更新:這裏是DateTimeHelper.NowUnspecified定義:

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

如果我DateTimeHelper.Now在我的測試通過的意見建議更換DateTimeHelper.NowUnspecified ......

Update2:在使用LinqPad隔離問題後,我意識到Entity Framework 6正在正確處理查詢。問題是與被拋出異常

感謝,

+0

什麼是'DateTimeHelper.NowUnspecified'? –

+0

@IvanStoev它是'DateTime'類型。非空的。我將其添加到說明中 –

+0

嘗試使用'DateTime.Now',看看它是否有幫助。 –

回答

1

的問題是,EF不知道如何將財產轉換爲SQL Effort.EF6庫。如果你確實需要使用該屬性(或運行在將來這樣的情況),您可以通過強制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(); 
+0

您可以將'.OrderByDescending(...)'移動到'.ToArray()'調用之前。你可以使用'.AsEnumerable()'而不是'.ToArray()',它可以防止實現集合。 – Maarten

+0

我想你可以通過這樣做來保存讀取整個查詢結果:'(from select new {})。OrderBy()。FirstOrDefault()'然後在你留下的單個記錄上進行投影。 – Maarten