我有这段代码:
IQueryable<Dealer> dealers = db.Dealers.Include(x => x.Statuses);
this.view.Table = dealers
.Select(x => new DealerRow
{
Id = x.Id,
LastStatus = x.Statuses.FirstOrDefault()
})
.ToList();
以this.view.Table = dealers
开头的行是发生此异常的地方:
对象引用未设置为对象的实例。
堆栈跟踪如下:
[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.Entity.SelectStatement.AddDefaultColumns(Scope scope) +293
MySql.Data.Entity.SelectStatement.Wrap(Scope scope) +36
MySql.Data.Entity.SelectGenerator.Visit(DbApplyExpression expression) +236
System.Data.Entity.Core.Common.CommandTrees.DbApplyExpression.Accept(DbExpressionVisitor`1 visitor) +64
MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +32
MySql.Data.Entity.SelectGenerator.HandleJoinExpression(DbExpressionBinding left, DbExpressionBinding right, DbExpressionKind joinType, DbExpression joinCondition) +77
MySql.Data.Entity.SelectGenerator.Visit(DbJoinExpression expression) +48
System.Data.Entity.Core.Common.CommandTrees.DbJoinExpression.Accept(DbExpressionVisitor`1 visitor) +64
MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +32
MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +22
MySql.Data.Entity.SelectGenerator.Visit(DbSortExpression expression) +76
System.Data.Entity.Core.Common.CommandTrees.DbSortExpression.Accept(DbExpressionVisitor`1 visitor) +64
MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +32
MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +22
MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +53
System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor`1 visitor) +64
MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +68
MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +328
System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext) +13
System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext) +127
System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory) +1420
System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext) +103
System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree, DbInterceptionContext interceptionContext) +127
System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree) +151
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) +161
System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +1027
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__6() +39
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) +288
System.Data.Entity.Core.Objects.<>c__DisplayClass7.<GetResults>b__5() +155
System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Func`1 operation) +9
System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +281
System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +11
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +45
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +387
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
DealersPresenter.RefreshTable() in File1.cs:98
DealersListPage.Filter(Object sender, EventArgs e) in File2.aspx.cs:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9659822
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
具体来说,如果我从选择中注释LastStatus
,程序编译并完美运行。所以我假设Statuses
是问题所在。
一对多关系设置如下:
[Table("dealers")]
public class Dealer
{
public Dealer()
{
Statuses = new List<DealerStatus>();
}
[Key]
[Column("id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ICollection<DealerStatus> Statuses { get; set; }
}
和
[Table("dealer_statuses")]
public class DealerStatus
{
[Key]
[Column("id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[ForeignKey("Dealer")]
[Column("id_dealer")]
public int DealerId { get; set; }
public virtual Dealer Dealer { get; set; }
[Required]
[Column("note")]
public string Note { get; set; }
}
我已经阅读了其他5个问题。我试着打电话给Include
和类似的人。我还检查了Dealer的默认构造函数初始化一个空列表。
造成这种异常的原因是什么?
答案 0 :(得分:0)
尝试从集合中移除virtual
以及其他类型的热切加载后,我在Select
调用之后移动了ToList
(这将会急切地加载所有内容)但是至少它编译并运行。
更新到最新的6.9.9 MySql.Data.Entity对我来说也不起作用。