【问题标题】:How to do a null check in a LINQ to Entities with SQL CE 4.0如何使用 SQL CE 4.0 在 LINQ to Entities 中进行空检查
【发布时间】: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)

【问题讨论】:

标签: linq linq-to-entities sql-server-ce sql-server-ce-4


【解决方案1】:

这是当前发布的 SQL Server Compact 实体框架提供程序中的一个错误,已在版本 6 https://entityframework.codeplex.com/workitem/287 中修复。 Emtity Framework 6 支持.NET 4.0,你想要的NuGet包是EntityFramework.SqlServerCompact

【讨论】:

  • 我搬到了 RC,但我仍然有错误。上面的堆栈跟踪。
  • 我已经隔离了这个错误,我的问题中有详细信息。
  • 有趣的信息,为什么不直接使用“”呢?
  • String.Empty 更明确一点,我想传递一个空字符串。在这种情况下,它们的工作方式相同,并且已经足够好了。我想我会在更严重的情况下遇到同样的错误,比如.DatePlaced = If(x.DatePlaced.HasValue, x.DatePlaced.Value, Date.Now)。最后的Date.Now 可能会崩溃,而不是在SQL 中变成GETDATE()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多