[英]EntityCommandExecutionException when returning a PagedList
Edit: I've solved the original problem, but it gives rise to another question 编辑:我已经解决了原来的问题,但它引起了另一个问题
The problem was with this line 问题在于这条线
&& (logLevel == "All" || logLevel == "Error")
The LINQ query in 'list' generated the following SQL “列表”中的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))
The error I was getting (Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'.") was caused by this part of the SQL 我收到的错误(内部异常=“ p_linq_2:字符串截断:max = 3,len = 5,value ='Error'。”)是由SQL的这一部分引起的
(N'All' = @p__linq__2)
When I removed logLevel == "All" from the LINQ query, the error disappeared. 当我从LINQ查询中删除logLevel ==“ All”时,错误消失了。
So, my question now is - why is LINQ/SQL attempting to truncate p_linq__2? 所以,我现在的问题是-为什么LINQ / SQL试图截断p_linq__2? It is performing a comparison... why did it need to truncate?
它正在执行比较...为什么需要截断?
Original Question 原始问题
I'm integrating Elmah and Log4Net with my existing MVC application (following this guide - http://www.codeproject.com/Articles/104112/Log-Reporting-Dashboard-for-ASP-NET-MVC ). 我正在将Elmah和Log4Net与我现有的MVC应用程序集成(遵循本指南-http: //www.codeproject.com/Articles/104112/Log-Reporting-Dashboard-for-ASP-NET-MVC )。 I'm using SQL CE 4. Handled and unhandled errors ARE being logged to the database, the issue I am having is with the UI to view the errors
我正在使用SQL CE4。处理和未处理的错误正在记录到数据库中,我遇到的问题是UI可以查看错误
I am getting an EntityCommandExecutionException triggered by this line (full method listing below) 我正在此行触发EntityCommandExecutionException(下面列出了完整的方法)
return new PagedList<LogEvent>(list, pageIndex, pageSize);
From what I can see, 'list' is populated OK.. 据我所知,“列表”已填充好..
The method where the error originates 错误产生的方法
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);
}
The method it calls 它调用的方法
private ILogReportingRepository GetProvider(string logProviderName)
{
string logSourceType = logProviders[logProviderName];
Type providerType = Type.GetType(logSourceType);
ILogReportingRepository provider = Activator.CreateInstance(providerType, _context) as ILogReportingRepository;
return provider;
}
The chained method 链式方法
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;
}
Stack trace 堆栈跟踪
[InvalidOperationException: p_ linq _2 : String truncation: 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 [InvalidOperationException:p_ linq _2:字符串截断:max = 3,len = 5,value ='Error'。] System.Data.SqlServerCe.SqlCeCommand.FillParameterDataDataBindings(Boolean verifyValue)+1670 System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand( CommandBehavior行为,String方法,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: An error occurred while executing the command definition. [EntityCommandExecutionException:执行命令定义时发生错误。 See the inner exception for details.] 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 query, Expression queryRoot) +25 System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +70 System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +82 System.Linq.Queryable.Count(IQueryable 1 source) +233 MvcPaging.PagedList
1..ctor(IQueryable 有关详细信息,请参见内部异常。] System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand实体命令,CommandBehavior行为)+507 System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext上下文,ObjectParameterCollection参数值)+675 System.Data。 Objects.ObjectQuery
1.GetResults(Nullable
1)+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(IEnumerable 1 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, 1 source, Int32 index, Int32 pageSize, Nullable
1 totalCount)+532 IDSM.Repository.LogReportingFacade.GetByDateRangeAndType(Int32 pageIndex,Int32 pageSize,DateTime开始,DateTime结束,
I've solved my problem, although it gives rise to another question. 我解决了我的问题,尽管这引起了另一个问题。
The problem was with this line 问题在于这条线
&& (logLevel == "All" || logLevel == "Error")
The LINQ query in 'list' generated the following SQL “列表”中的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))
The error I was getting (Inner exception = "p_linq_2 : String truncation: max=3, len=5, value='Error'.") was caused by this part of the SQL 我收到的错误(内部异常=“ p_linq_2:字符串截断:max = 3,len = 5,value ='Error'。”)是由SQL的这一部分引起的
(N'All' = @p__linq__2)
When I removed logLevel == "All" frmo the LINQ query, the error disappeared. 当我在LINQ查询中删除logLevel ==“ All”时,错误消失了。
My next question is - why LINQ/SQL attempting to truncate p_linq__2? 我的下一个问题是-为什么LINQ / SQL试图截断p_linq__2? It was performing a comparison... why did it need to truncate?
它正在执行比较...为什么需要截断?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.