我有一个Code First模型:我正在尝试使用Include()
和IncludeThen()
并获得System.ArgumentNullException
。
以下是实体(如果您需要更多模型,请告诉我们):
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 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, Func
3 projectionAdder,IQuerySource querySource) 在Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.d__13.MoveNext() 在System.Collections.Generic.List1..ctor(IEnumerable
1个集合) 在System.Dynamic.Utils.CollectionExtensions.ToReadOnly [T](IEnumerable1 enumerable) at System.Linq.Expressions.Expression.NewArrayInit(Type type, IEnumerable
1个初始值设定项) at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.IncludeExpressionVisitor.VisitMethodCall(MethodCallExpression expression) 在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) 在Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel,IReadOnlyCollection1 includeSpecifications) at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.IncludeNavigations(QueryModel queryModel, IReadOnlyCollection
1 includeSpecifications) at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.IncludeNavigations(QueryModel queryModel) 在Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor [TResult](QueryModel queryModel) 在Microsoft.Data.Entity.Storage.Database.CompileQuery [TResult](QueryModel queryModel) ---从抛出异常的先前位置开始的堆栈跟踪结束--- 在Microsoft.Data.Entity.Query.Internal.QueryCompiler。&lt;&gt; c__DisplayClass18_01.<CompileQuery>b__0() at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func
1编译器) 在Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery [TResult](表达式查询) 在Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute [TResult](表达式查询) 在Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute [TResult](表达式表达式) 在Remotion.Linq.QueryableBase1.GetEnumerator() at Microsoft.Data.Entity.EntityFrameworkQueryableExtensions.IncludableQueryable
2.GetEnumerator() 在WeatherMonitoringConsole.Program。&lt;&gt; c__DisplayClass0_0。&lt; b__0&gt; d.MoveNext()在C:\ Users \ ehasson \ Source \ Workspaces \ Marketing \ WeatherMonitoring \ WeatherMonitoringConsole \ Program.cs:line 32
答案 0 :(得分:3)
问题是我需要在每个实体上使用默认构造函数。