c# - MySQL - Entity Framework 的问题

标签 c# mysql .net entity-framework

此查询有效。

string value = null;
var temp1 = (from d in db.Device
             where                                 
                (value == null ? true : d.Status == 1)
              select d).ToList();

但是这个查询不起作用。

string value = null;
var temp1 = (from d in db.Device
             where                                 
                (value == null ? true : d.Status == 1)
              select d).OrderBy(i => i.Title).ToList();

Device 模型具有 StatusTitle 属性。

错误:未将对象引用设置为对象的实例。

MySql.Data版本:6.9.9

连接器/网络:6.9.9

MySql.Data.Entity.EF6

只需添加 orderby 即可得到错误。你能告诉我为什么吗?

堆栈跟踪:

  at MySql.Data.Entity.CaseFragment.Accept(SqlFragmentVisitor visitor) at MySql.Data.Entity.BinaryFragment.Accept(SqlFragmentVisitor visitor) at MySql.Data.Entity.SqlGenerator.FuseSelectWithInnerSelect(SelectStatement outer, SelectStatement inner)    at MySql.Data.Entity.SqlGenerator.TryFusingSelect(InputFragment f)    at MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type)    at MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type)    at MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression)    at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType](DbExpressionVisitor`1 visitor)    at MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree)    at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)    at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)    at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)    at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)    at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)    at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext)    at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)    at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)    at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)    at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)    at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)    at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)    at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

最佳答案

根据堆栈跟踪,这是 MySQL 连接器在处理 时的错误? :操作符部分(CaseFragment)。

这导致我们通过消除 找到解决方法? :构造

(value == null ? true : d.Status == 1)

与同等

(value == null || d.Status == 1)

关于c# - MySQL - Entity Framework 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44887148/

相关文章:

c# - 将 UTC 时间转换为实际有效的本地国家时间

c# - 是否可以从 DetailsView 中的 CheckboxField 中提取 Checked 状态?

c# - 在回调中重用回调时如何防止 StackOverflowException?

php - {} 如何影响 PHP 中的 MySQL 查询?

c# - 为什么 RequestAdditionalTime() 方法在 Vista/7 中重启时不起作用?

c# - C++ -> C# 使用 SWIG : how to use default constructor for a class

php - 如何根据总分计算排名

php - 返回最后插入的 id

c# - 你能用 C# 编写单元测试程序集来测试用 VB 编写的程序集吗?

c# - 字符串匹配