编辑:我已经解决了原来的问题,但它引起了另一个问题
问题出在这条线上
&& (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))
我得到的错误 (Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'.") 是由这部分 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);
- 错误消息=“执行命令定义时发生错误。有关详细信息,请参阅内部异常。”
- 内部异常 =“p_linq_2 :字符串截断:max=3,len=5,value='Error'。”
据我所见,“列表”已填充好..
错误产生的方法
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 行为、字符串方法、ResultSetOptions 选项)+397 System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior 行为)+59 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior 行为)+342 System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader (CommandBehavior 行为)+41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为)+10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior 行为)+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 源) +100 System.Data.Objects.ELinq.ObjectQueryProvider.b__3(IEnumerable1 sequence) +5
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable
1 查询,表达式 queryRoot)+25 System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(表达式表达式)+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,