返回 PagedList 时的 EntityCommandExecutionException
本文关键字:EntityCommandExecutionException 时的 PagedList 返回 | 更新日期: 2023-09-27 18:17:35
编辑:我已经解决了原始问题,但它引起了另一个问题
问题出在这条线上
&& (logLevel == "All" || logLevel == "Error")
"列表"中的 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,值='错误'(。
(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 查看错误
我收到由此行触发的实体命令执行异常(下面列出了完整的方法(
return new PagedList<LogEvent>(list, pageIndex, pageSize);
- 错误消息 ="执行命令时出错定义。有关详细信息,请参阅内部异常。
- 内部异常 ="p_linq_2 :字符串截断:max=3,len=5,值='错误'。
据我所知,"列表"填充正常。
产生错误的方法
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( +1670System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options( +397System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior( +59System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior( +342System.Data.SqlServerCe.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior( +41System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior( +10System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommand(EntityCommand entityCommand, CommandBehavior behavior( +437
[实体命令执行异常:执行命令定义时出错。有关详细信息,请参阅内部异常。System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommand(EntityCommand entityCommand, CommandBehavior behavior( +507System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues( +675System.Data.Objects.ObjectQuery 1.GetResults(Nullable
1 forMergeOption( +102System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +30
System.Linq.Enumerable.Single(IEnumerable
1 个源( +100System.Data.Objects.ELinq.ObjectQueryProvider.b__3(IEnumerable 1 sequence) +5
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable
1 个查询,表达式查询根( +25System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(表达式表达式( +70System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(表达式表达式( +82System.Linq.Queryable.Count(IQueryable 1 source) +233
MvcPaging.PagedList
1..ctor(IQueryable 1 source, Int32 index, Int32 pageSize, Nullable
1 totalCount( +532IDSM。Repository.LogReportingFacade.GetByDateRangeAndType(Int32 pageIndex, Int32 pageSize, DateTime start, DateTime end,
我已经解决了我的问题,尽管它引起了另一个问题。
问题出在这条线上
&& (logLevel == "All" || logLevel == "Error")
"列表"中的 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,值='错误'(。
(N'All' = @p__linq__2)
当我从 LINQ 查询中删除 logLevel == "All" 时,错误消失了。
我的下一个问题是 - 为什么 LINQ/SQL 试图截断p_linq__2? 它正在执行一个比较...为什么需要截断?