我目前正在遷移從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.MetaDateCreated
和employeeWorkLocation.MetaDateUpdated
都是可空Datetime?
型
DateTimeHelper.NowUnspecified
的是Datetime
非空類型。相同MetaDateCreated
和MetaDateUpdated
任何想法?這是工作的罰款與實體框架5
更新:這裏是DateTimeHelper.NowUnspecified
定義:
public static DateTime NowUnspecified
{
get
{
return DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified);
}
}
如果我DateTimeHelper.Now
在我的測試通過的意見建議更換DateTimeHelper.NowUnspecified
......
Update2:在使用LinqPad隔離問題後,我意識到Entity Framework 6正在正確處理查詢。問題是與被拋出異常
感謝,
什麼是'DateTimeHelper.NowUnspecified'? –
@IvanStoev它是'DateTime'類型。非空的。我將其添加到說明中 –
嘗試使用'DateTime.Now',看看它是否有幫助。 –