编辑:我已经解决了原来的问题,但它引发了另一个问题
问题在于这一行
&& (logLevel == "All" || logLevel == "Error")
'list'中的LINQ查询生成以下SQL
SELECT
0 AS [C1],
N'guid' AS [C2],
[Extent1].[ErrorId] AS [ErrorId],
[Extent1].[TimeUtc] AS [TimeUtc]
FROM [ELMAH_Error] AS [Extent1]
WHERE ([Extent1].[TimeUtc] >= @p__linq__0) AND ([Extent1].[TimeUtc] <= @p__linq__1) AND ((N'All' = @p__linq__2) OR (N'Error' = @p__linq__3))
我得到的错误(内部异常=“p_linq_2:字符串截断:max = 3,len = 5,value ='错误'。”)是由SQL的这一部分引起的
(N'All' = @p__linq__2)
当我从LINQ查询中删除logLevel ==“All”时,错误消失了。
所以,我现在的问题是 - 为什么LINQ / SQL试图截断p_linq__2?它正在进行比较......为什么需要截断?
原始问题
我正在将Elmah和Log4Net与我现有的MVC应用程序集成(遵循本指南 - http://www.codeproject.com/Articles/104112/Log-Reporting-Dashboard-for-ASP-NET-MVC)。我正在使用SQL CE 4.已处理和未处理的错误正在记录到数据库中,我遇到的问题是使用UI来查看错误
我收到此行触发的EntityCommandExecutionException(下面的完整方法列表)
return new PagedList<LogEvent>(list, pageIndex, pageSize);
从我所看到的,'list'填充OK ..
发生错误的方法
public IPagedList<LogEvent> GetByDateRangeAndType(int pageIndex, int pageSize, DateTime start, DateTime end, string logProviderName, string logLevel)
{
IQueryable<LogEvent> list = null;
switch (logProviderName)
{
case "All":
foreach (string providerName in logProviders.Keys)
{
IQueryable<LogEvent> logList = GetProvider(providerName).GetByDateRangeAndType(pageIndex, pageSize, start, end, logLevel);
list = (list == null) ? logList : list.Union(logList);
}
break;
default:
list = GetProvider(logProviderName).GetByDateRangeAndType(pageIndex, pageSize, start, end, logLevel);
break;
}
list = list.OrderByDescending(d => d.LogDate);
return new PagedList<LogEvent>(list, pageIndex, pageSize);
}
它调用的方法
private ILogReportingRepository GetProvider(string logProviderName)
{
string logSourceType = logProviders[logProviderName];
Type providerType = Type.GetType(logSourceType);
ILogReportingRepository provider = Activator.CreateInstance(providerType, _context) as ILogReportingRepository;
return provider;
}
链式方法
public IQueryable<LogEvent> GetByDateRangeAndType(int pageIndex, int pageSize, DateTime start, DateTime end, string logLevel)
{
IQueryable<LogEvent> list = (from a in _context.ELMAH_Error
where a.TimeUtc >= start && a.TimeUtc <= end
&& (logLevel == "All" || logLevel == "Error")
select new LogEvent
{
IdType = "guid"
,
Id = ""
,
IdAsInteger = 0
,
IdAsGuid = a.ErrorId
,
LoggerProviderName = "Elmah"
,
LogDate = a.TimeUtc
,
MachineName = a.Host
,
Message = a.Message
,
Type = a.Type
,
Level = "Error"
,
Source = a.Source,
StackTrace = ""
});
return list;
}
堆栈跟踪
[InvalidOperationException:p_ linq _2:字符串截断:max = 3,len = 5,value ='Error'。] System.Data.SqlServerCe.SqlCeCommand.FillParameterDataBindings(Boolean verifyValue)+1670 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior,String method,ResultSetOptions options)+397 System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior)+59 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior)+342 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior)+41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)+10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+437
[EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常。]
System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)+507
System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context,ObjectParameterCollection parameterValues)+675
System.Data.Objects.ObjectQuery 1.GetResults(Nullable
1 forMergeOption)+102
System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +30
System.Linq.Enumerable.Single(IEnumerable
1 source)+100
System.Data.Objects.ELinq.ObjectQueryProvider.b__3(IEnumerable 1 sequence) +5
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable
1个查询,表达式queryRoot)+25
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression)+70
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(表达式表达式)+82
System.Linq.Queryable.Count(IQueryable 1 source) +233
MvcPaging.PagedList
1..ctor(IQueryable 1 source, Int32 index, Int32 pageSize, Nullable
1 totalCount)+532
IDSM.Repository.LogReportingFacade.GetByDateRangeAndType(Int32 pageIndex,Int32 pageSize,DateTime start,DateTime end,
答案 0 :(得分:0)
我已经解决了我的问题,虽然它引出了另一个问题。
问题在于这一行
&& (logLevel == "All" || logLevel == "Error")
'list'中的LINQ查询生成以下SQL
SELECT
0 AS [C1],
N'guid' AS [C2],
[Extent1].[ErrorId] AS [ErrorId],
[Extent1].[TimeUtc] AS [TimeUtc]
FROM [ELMAH_Error] AS [Extent1]
WHERE ([Extent1].[TimeUtc] >= @p__linq__0) AND ([Extent1].[TimeUtc] <= @p__linq__1) AND ((N'All' = @p__linq__2) OR (N'Error' = @p__linq__3))
我得到的错误(内部异常=“p_linq_2:字符串截断:max = 3,len = 5,value ='错误'。”)是由SQL的这一部分引起的
(N'All' = @p__linq__2)
当我删除了LINQ查询中的logLevel ==“All”时,错误消失了。
我的下一个问题是 - 为什么LINQ / SQL试图截断p_linq__2?它正在进行比较......为什么需要截断?