[英]Exception when loading related objects. Entity Framework
I am getting an exception when loading related objects in my db. 我在db中加载相关对象时遇到异常。 I am loading all my
MatchData
objects and I want to iterate them with a foreach. 我正在加载我的所有
MatchData
对象,我想用foreach迭代它们。
The objects that I'm loading is: 我正在加载的对象是:
MatchData Class: MatchData类:
public class MatchData
{
[Key]
public virtual int Id { get; set; }
private List<PlayerData> blueTeam = new List<PlayerData>();
private List<PlayerData> redTeam = new List<PlayerData>();
[InverseProperty("MatchDataBlue")]
public virtual List<PlayerData> BlueTeam
{
get { return blueTeam; }
set { blueTeam = value; }
}
[InverseProperty("MatchDataRed")]
public virtual List<PlayerData> RedTeam
{
get { return redTeam; }
set { redTeam = value; }
}
}
PlayerData Class: PlayerData类:
public class PlayerData
{
// properties
[Key]
public virtual int Id { get; set; }
public virtual Player Player { get; set; }
public virtual MatchData MatchDataBlue { get; set; }
public virtual MatchData MatchDataRed { get; set; }
}
Here is how I load them the MatchData objects: 以下是我如何加载MatchData对象:
using (DBBooneContext db = new DBBooneContext())
{
var smth = db.MatchData
.Include(md => md.BlueTeam)
.ToList();
}
DBContext 的DbContext
class DBBooneContext : DbContext
{
public DbSet<Player> Player { get; set; }
public DbSet<PlayerData> PlayerData { get; set; }
public DbSet<MatchData> MatchData { get; set; }
}
When I run the ToList i get the exception: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll 当我运行ToList时,我得到异常:EntityFramework.SqlServer.dll中发生了未处理的异常类型'System.Reflection.TargetInvocationException'
full exception: 完全例外:
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in EntityFramework.SqlServer.dll
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The class 'Boonekamp.ClassCollection.PlayerData' has no parameterless constructor.
at System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorForType(Type type)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Emit_ConstructEntity(EntityType oSpaceType, IEnumerable`1 propertyBindings, Expression entityKeyReader, Expression entitySetReader, TranslatorArg arg, EntityProxyTypeInfo proxyTypeInfo)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(EntityColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.HandleSpandexRecord(RecordColumnMap columnMap, TranslatorArg arg, RowType spanRowType)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(RecordColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Query.InternalTrees.RecordColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(DiscriminatedCollectionColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Query.InternalTrees.DiscriminatedCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.AcceptWithMappedType(TranslatorVisitor translatorVisitor, ColumnMap columnMap)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.HandleSpandexRecord(RecordColumnMap columnMap, TranslatorArg arg, RowType spanRowType)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(RecordColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Query.InternalTrees.RecordColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg)
at System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap[T](ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(Translator translator, Type elementType, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
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.EntitySqlQueryState.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.SqlServer.DefaultSqlExecutionStrategy.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)
at Boonekamp.ClassCollection.Player.getStats(Predicate`1 predicate) in d:\Dropbox\code\c#\Boonekamp\Boonekamp\ClassCollection\Player.cs:line 49
Additional information: Exception has been thrown by the target of an invocation.
I have absolutely no clue how to solve this issue. 我完全不知道如何解决这个问题。 Any ideas?
有任何想法吗?
The inner exception says it all: 内在的例外说明了一切:
The class 'Boonekamp.ClassCollection.PlayerData' has no parameterless constructor
类'Boonekamp.ClassCollection.PlayerData'没有无参数构造函数
Change your PlayerData
to: 将您的
PlayerData
更改为:
[Obsolete("Only needed for serialization and materialization", true)]
public PlayerData() {}
public PlayerData(Player player)
{
}
This way, you do have a parameter-less constructor that Entity Framework will be able to use during initialization. 这样,您就可以使用Entity Framework在初始化期间可以使用的无参数构造函数。 Yet, you prevent using that constructor in code using the
[Obsolete]
attribute. 但是,您使用
[Obsolete]
属性阻止在代码中使用该构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.