Exception lors du chargement d'objets liés. Entity Framework

Je suis une exception lors du chargement d'objets liés dans ma db. Je suis le chargement de toutes mes MatchData objets et je tiens à réitérer avec un foreach.

Les objets que je suis le chargement est:

MatchData Classe:

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 Classe:

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; }
}

Voici comment j'ai charger le MatchData objets:

using (DBBooneContext db = new DBBooneContext())
{
    var smth = db.MatchData
        .Include(md => md.BlueTeam)
        .ToList();
}

DBContext

class DBBooneContext : DbContext
{
    public DbSet<Player> Player { get; set; }
    public DbSet<PlayerData> PlayerData { get; set; }
    public DbSet<MatchData> MatchData { get; set; }
}

Quand je lance le ToList-je obtenir de l'exception:
Une exception non gérée du type 'System.De la réflexion.TargetInvocationException' s'est produite dans EntityFramework.SqlServer.dll

plein 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.

Je n'ai absolument aucune idée de comment résoudre ce problème. Des idées?

Il semble étrange qu'un appel à ToList() serait la cause d'une invocation d'erreur dans EntityFramework.SqlServer.dll. Ma conjecture est que c'est un problème différent. Pouvez-vous montrer votre DBBooneContext de mise en œuvre (y compris les conventions et/ou des configurations que vous avez défini)?
La queation est mis à jour pour vous 🙂
Malheureusement, il n'est pas comme illuminant comme je l'avais espéré. Je me demande si il ya un sous-jacent DB question. Quel est votre base de données de schéma? Ne changera rien si vous supprimez le Include ou ToList des appels à partir de votre using bloc?
Est-il une exception interne, par hasard? Pouvez-vous fournir la pleine puissance de l'exception (ex.ToString())?
Il semble qu'il est la .Comprennent qui est à l'origine du problème. Quand je n'exécutez tostring() et() je n'obtiens pas l'exception. Je ne suis pas sûr de la façon de vous montrer le système lors de l'utilisation de code de la première.

OriginalL'auteur Mads Gadeberg | 2014-03-26