【发布时间】:2013-10-02 08:57:38
【问题描述】:
我有一个 LINQ 查询:
Dim result = DataContext.Items.Select(Function(p) New With {
.Category = If(p.Category IsNot Nothing, p.Category.Name, String.Empty)
.Name = p.Name
})
这适用于 SQL Server 2005,2008,2012 数据库。当我针对 SQL CE 4.0 DB 运行它时,我得到了这个异常:
异常详细信息:System.Data.SqlServerCe.SqlCeException:指定的参数 该函数的值无效。 [ 参数 # = 3,名称 函数(如果已知)= 案例]
我已将问题隔离到String.Empty。如果我直接使用"",代码运行良好。如果我使用.Category = String.Empty,它也可以工作。似乎 SqlCe 如何处理 If MethodCallExpression 内的 MemberAccessExpression 的错误。
如果我取出.Category 行,一切正常。在 LINQ to SQL CE 4 中进行空检查的正确方法是什么?我检查了Skeet's answer,但实际上我想检查数据库值,而不是我的值。我也见过this answer,但它会将所有内容加载到内存中,这会破坏使用 LINQ 的目的。
升级到 EF 6 RC 后我仍然有异常(根据 ErikEJ 的建议)。这是一个堆栈跟踪:
at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
at System.Data.SqlServerCe.SqlCeCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.SqlServerCompact.SqlCeMultiCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8()
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
【问题讨论】:
-
我认为这是 SQLCE 的 EF 提供程序中的一个错误,已在版本 6 中修复 entityframework.codeplex.com/workitem/287
-
@ErikEJ 我在 .NET 4.0 上,无法升级到 4.5。有什么办法可以修复吗? (AFAIK EF 4.3+ 需要 .NET 4.5)。
-
实体框架 6 适用于 .NET 4
-
我刚刚找到了这样说的文件。这太棒了。
-
需要使用预发布的EntityFramework.SqlServerCompact包nuget.org/packages/EntityFramework.SqlServerCompact/6.0.0-rc1
标签: linq linq-to-entities sql-server-ce sql-server-ce-4