MySQL - 实体框架的问题

时间:2017-07-03 13:43:57

标签: 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属性。

错误:Object reference not set to an instance of an object.

MySql.Data版本:6.9.9

Connector/Net: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)

2 个答案:

答案 0 :(得分:3)

根据堆栈跟踪,处理? :运算符部分(CaseFragment)时出现MySQL连接器错误。

通过消除? :构造

,我们找到了解决方法
(value == null ? true : d.Status == 1)

与等效

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

答案 1 :(得分:1)

看起来您正在尝试将全包查询重写为LINQ。您需要使用LINQ编写全部查询,如果您不想要它,您可以简单地忽略该条件,例如:

string value=someOptionalParameter;
var query = (value==null)? db.Device : db.Where(d=>d.Status == 1);    
var temp1 =  query.OrderBy(d=>d.Title).ToList();

在这种情况下,根本不使用value。它只是用作旗帜。如果 希望使用它,例如可选择按Category字段进行过滤,则可以以相同的方式在where子句中使用它:

string value=someOptionalParameter;
var query = (value==null)? db.Device : db.Where(d=>d.Category == value);    
var temp1 =  query.OrderBy(d=>d.Title).ToList();

通常,您可以逐位“构建”查询。在您尝试枚举和/或将其转换为List,Array等之前,IQueryable< T>不会转换为SQL。您可以从用户的选项中动态创建查询,如下所示:

var query = dbContext.MyEntity;
if (someField != null)
{
    query = query.Where(d=>d.SomeField = someField);
}

if (someOtherField != null)
{
    query = query.Where(d=>d.SomeOtherField = someField);
}

if (mustOrder != null)
{
    query = query.OrderBy(d=>d.OrderField);
}

//Execute the final query
return query.ToList();