使用IncludeThen时出现System.ArgumentNullException [英] System.ArgumentNullException when using IncludeThen

查看:70
本文介绍了使用IncludeThen时出现System.ArgumentNullException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码优先模型:我正在尝试使用 Include() IncludeThen ()并获得 System.ArgumentNullException

I have an Code First model: I am attempting to use Include() and IncludeThen() and get a System.ArgumentNullException .

以下是实体(我知道您是否需要更多模型):

Here are the entities (let me know if you'd like more of the model):

 public class Area
{
    public Area()
    {
        Geocode = new List<Geocode>();
    }

    public int AreaId { get; set; }
    public int InfoId { get; set; }
    public string AreaDescription { get; set; }
    public string Polygon { get; set; }
    public string Circle { get; set; }
    public List<Geocode> Geocode { get; set; }
    public string Altitude { get; set; }
    public string Ceiling { get; set; }
}

    public class Geocode
{
    public Geocode(string valueName, string value
        )
    {
        ValueName = valueName;
        Value = value;
    }

    public int GeocodeId { get; set; }
    public int AreaId { get; set; }
    public string ValueName { get; set; }
    public string Value { get; set; }
}

这里是调用代码:

context.Alerts.Include(f => f.Infos)
                .ThenInclude(f => f.Areas)
                .ThenInclude(f => f.Geocode);// When I comment out this line it does not error, just doesn't load the Geocode navigation property.

这里是堆栈跟踪:


at System.Linq.Expressions.Expression.New(ConstructorInfo构造函数,IEnumerable 1个参数)
在Microsoft.Data.Entity.Metadata.Internal.EntityMaterializerSource.CreateMaterializeExpression( IEntityType实体类型,表达式valueBufferExpression,Int32 [] indexMap)
在Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.MaterializerFactory.CreateMaterializer(IEntityType实体类型,SelectExpression selectExpression,Func querySource)Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.d__13.MoveNext()
在System.Collections.Generic.List 1..ctor(IEnumerable 1集合)
,位于System.Dynamic.Utils.CollectionExtensions.ToReadOnly [T](IEnumerable 1可枚举)
,位于System.Linq.Expressions.Expression .NewArrayInit(Type type,IEnumerable
1初始设置lizers)Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.VisitMethodCall(MethodCallExpression expression)
在System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor访问者)
在Microsoft .Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表达式表达式)Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(IncludeSpecification includeSpecification,Type resultType,LambdaExpression accessorLambda,Boolean querySourceRequiresTracking)
Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(QueryModel queryModel,IReadOnlyCollection 1 includeSpecifications)
$ c> 1个includeSpecifications)
,位于Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel)$ b Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor [TResult](QueryModel queryModel)中的$ b Microsoft.Data.Entity.Storage.Database.CompileQuery [TResult](QueryModel queryModel)
- -从之前引发异常的位置开始的堆栈跟踪--- Microsoft.Data.Entity.Query.Internal.QueryCompiler上的
。<> c__DisplayClass18_0 1。< CompileQuery> b__0( )
在Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery [TResult](Object cacheKey,Func
1编译器)
在Microsoft.Data.Entity.Query。 Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute [TResult](表达式查询)
,Microsoft.Data.Entity.Query的Internal.QueryCompiler.CompileQuery [TResult](表达式查询)
。 Internal.EntityQueryProvider.Execute [TResult](表达式)
在Remotion.Linq.QueryableBase 1.GetEnumerator()
在Microsoft.Data.Entity.EntityFrameworkQueryableExtensions.In cludableQueryable
2.GetEnumerator()
在WeatherMonitoringConsole.Program。<> c__DisplayClass0_0。< b__0> d.MoveNext()在C:\Users\ehasson\Source\工作区\市场营销\WeatherMonitoring\WeatherMonitoringConsole\Program.cs:line 32

at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable1 arguments) at Microsoft.Data.Entity.Metadata.Internal.EntityMaterializerSource.CreateMaterializeExpression(IEntityType entityType, Expression valueBufferExpression, Int32[] indexMap) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.MaterializerFactory.CreateMaterializer(IEntityType entityType, SelectExpression selectExpression, Func3 projectionAdder, IQuerySource querySource) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.d__13.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable1 enumerable) at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable1 initializers) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.VisitMethodCall(MethodCallExpression expression) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(IncludeSpecification includeSpecification, Type resultType, LambdaExpression accessorLambda, Boolean querySourceRequiresTracking) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel, IReadOnlyCollection1 includeSpecifications) at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(QueryModel queryModel, IReadOnlyCollection1 includeSpecifications) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel) --- End of stack trace from previous location where exception was thrown --- at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_01.<CompileQuery>b__0() at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase1.GetEnumerator() at Microsoft.Data.Entity.EntityFrameworkQueryableExtensions.IncludableQueryable2.GetEnumerator() at WeatherMonitoringConsole.Program.<>c__DisplayClass0_0.<b__0>d.MoveNext() in C:\Users\ehasson\Source\Workspaces\Marketing\WeatherMonitoring\WeatherMonitoringConsole\Program.cs:line 32


推荐答案

问题是我需要在每个实体上使用默认构造函数。

The problem was I needed a default constructor on each entity.

这篇关于使用IncludeThen时出现System.ArgumentNullException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆