2009-04-07 5 views
3

OK, je suis en train d'utiliser Entity Framework, mais je l'ai fait fonctionner, mais j'ai maintenant ajouté plusieurs entités au modèle, et maintenant je suis Obtenir l'erreur ci-dessus Pour le débogage, j'ai créé un formulaire de test avec un contrôle de grille à remplir à partir de ma collection d'entités Projects.J'utilise des GUID pour mes clés primaires.J'ai seulement un bit de code:Entity Framework Exception: l'index était en dehors des limites du tableau. "

Public Class TestForm 
Private Sub TestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Me.ProjectBindingSource.DataSource = My.Application.Context.Projects 
End Sub 
End Class 

Voici le détail d'exception:

Exception interne: { "index était en dehors des limites du tableau."} Source: « System.Data.Entit y "

System.Data.EntityCommandCompilationException était unhandled message =" Une erreur est survenue lors de la préparation de la définition de la commande. Voir l'exception interne pour plus de détails « Source = "System.Data.Entity" StackTrace:. à System.Data.EntityClient.EntityCommandDefinition..ctor (DbProviderFactory storeProviderFactory, DbCommandTree commandTree) à System.Data.EntityClient.EntityProviderServices. CreateCommandDefinition (DbProviderFactory storeProviderFactory, DbCommandTree commandTree) à System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition (DbProviderManifest providerManifest, DbCommandTree commandTree) à System.Data.Common.DbProviderServices.CreateCommandDefinition (DbCommandTree commandTree) à System.Data.Objects.Internal .ObjectQueryExecutionPlan.Prepare (contexte ObjectContext, arborescence DbQueryCommandTree, Type elementType, mergeOption mergeOption, span span) à System.Data.Objects.EntitySqlQueryState.GetExecutionPlan (nullables 1 forMergeOption) at System.Data.Objects.ObjectQuery 1.GetResults (Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery 1.GetIListSourceListInternal() à System.Data.Objects.ObjectQuery.System.ComponentModel.IListSource.GetList() à System.Windows. Forms.ListBindingHelper.GetList (Liste d'objets) à System.Windows.Forms.ListBindingHelper.GetList (Object dataSource, String dataMember) à System.Windows.Forms.BindingSource.ResetList() à System.Windows.Forms.BindingSource. set_DataSource (Valeur d'objet) at TestApp.TestForm.TestForm_Load (Expéditeur d'objet, EventArgs e) dans C: \ Users \ JoshPeltier.TFEARTH \ Documents \ Visual Studio 2008 \ Projets \ TestApp \ TestApp \ Forms \ TestForm.vb: ligne 3 at System.EventHandler.Invoke (Expéditeur d'objet, EventA rgs e) à System.Windows.Forms.Form.OnLoad (EventArgs e) à System.Windows.Forms.Form.OnCreateControl() à System.Windows.Forms.Control.CreateControl (Boolean fIgnoreVisible) au système. Windows.Forms.Control.CreateControl() à System.Windows.Forms.Control.WmShowWindow (message & m) à System.Windows.Forms.Control.WndProc (message & m) à System.Windows.Forms.ScrollableControl .WndProc (message & m) à System.Windows.Forms.ContainerControl.WndProc (message & m) à System.Windows.Forms.Form.WmShowWindow (message & m) à System.Windows.Forms.Form.WndProc (message & m) à System.Windows.Forms.Control.ControlNativeWindow.OnMessage (message & m) à System.Windows.Forms.Control.ControlNativeWindow.WndProc (message & m) à System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) à System.Windows.Forms.SafeNativeMethods.ShowWindow (HWND HandleRef, Int32 nCmdShow) à System.Windows.Forms.Control.SetVisibleCore (valeur booléenne) à System.Windows.Forms.Form.SetVisibleCore (valeur booléenne) à System.Windows.Forms.Control.set_Visible (valeur booléenne) à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (en raison Int32, contexte ApplicationContext) à System.Windows.Forms.Application.ThreadContext.RunMessageLoop (en raison Int32, contexte ApplicationContext) à System.Windows.Forms .Application.Run (contexte ApplicationContext) à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at Micros oft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run (String [] commandLine) à TestApp.My.MyApplication.Main (String [] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb: ligne 81 à System.AppDomain ._nExecuteAssembly (assemblage de montage, String [] args) à System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ThreadHelper .ThreadStart_Context (état de l'objet) à System.Threading.ExecutionContext.Run (ExecutionContext ExecutionContext, ContextCallback rappel, l'état de l'objet) à System.Threading.ThreadHelper.ThreadStart() InnerException: System.IndexOutOfRangeException Message = "L'index était en dehors des limites du tableau." Source = "System.Data.Entity" StackTrace: à System.Data.Mapping.ViewGeneration.Structures.CellQuery.CreateFieldAlignedCellQueries (CellQuery otherQuery, MemberPathMapBase projectedSlotMap, CellQuery & newMainQuery, CellQuery & newOtherQuery) à System.Data.Mapping .ViewGeneration.CellNormalizer.AlignFields (IEnumerable 1 cells, MemberPathMapBase projectedSlotMap, ViewTarget viewTarget) at System.Data.Mapping.ViewGeneration.CellNormalizer.CreateLeftCellWrappers(IEnumerable 1 extentCells, viewTarget viewTarget) à System.Data.Mapping.ViewGeneration.CellNormalizer..ctor (EntitySetBase mesure, IEnumerable 1 extentCells, SchemaContext schemaContext, CqlIdentifiers identifiers, ConfigViewGenerator config, MemberDomainMap queryDomainMap, MemberDomainMap updateDomainMap, StorageEntityContainerMapping entityContainerMapping, MetadataWorkspace workspace) at System.Data.Mapping.ViewGeneration.ViewGenerator.GetCellNormalizer(EntitySetBase extent, SchemaContext schemaContext, CqlIdentifiers identifiers) at System.Data.Mapping.ViewGeneration.ViewGenerator.GenerateViewsForExtent(SchemaContext schemaContext, EntitySetBase extent, CqlIdentifiers identifiers, KeyToListMap 2 fois) à System.Data.Mapping.ViewGeneration.ViewGenerator. GenerateViewsForSchemaContext (SchemaContext schemaContext, identificateurs CqlIdentifiers, KeyToListMap 2 views) at System.Data.Mapping.ViewGeneration.ViewGenerator.GenerateAllViews(KeyToListMap 2 vues, CqlIdentifier s identificateurs) à System.Data.Mapping.ViewGeneration.ViewGenerator.GenerateViewsForSchemaCells (Liste 1 cells, MetadataWorkspace workSpace, ConfigViewGenerator config, CqlIdentifiers identifiers, StorageEntityContainerMapping containerMapping) at System.Data.Mapping.ViewGeneration.ViewGenerator.GenerateViewsForSchema(StorageEntityContainerMapping containerMapping, MetadataWorkspace workSpace, ConfigViewGenerator config) at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGenerateViews(EntityContainer container, Dictionary 2 resultDictionary) à System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGetGeneratedViews (Conteneur à EntityContainer) à System.Data.Common.Utils.Memoizer 2.<>c__DisplayClass2.<Evaluate>b__0() at System.Data.Common.Utils.Memoizer 2.Result.GetValue() à System.Data.Common.Utils.Memoizer 2.Evaluate(TArg arg) at System.Data.Mapping.StorageMappingItemCollection.ViewDictionary.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace, StorageMappingItemCollection storageMappingItemCollection) at System.Data.Metadata.Edm.MetadataWorkspace.GetGeneratedView(EntitySetBase extent) at System.Data.Query.PlanCompiler.PreProcessor.ExpandView(Node node, ScanTableOp scanTableOp, IsOfOp& typeFilter) at System.Data.Query.PlanCompiler.PreProcessor.ProcessScanTable(Node scanTableNode, ScanTableOp scanTableOp, IsOfOp& typeFilter) at System.Data.Query.PlanCompiler.PreProcessor.Visit(ScanTableOp op, Node n) at System.Data.Query.InternalTrees.ScanTableOp.Accept[TResultType](BasicOpVisitorOfT 1 v, noeud N) à System.Data.Query.InternalTrees.BasicOpVisitorOfT 1.VisitNode(Node n) at System.Data.Query.PlanCompiler.PreProcessor.VisitChildren(Node n) at System.Data.Query.PlanCompiler.PreProcessor.VisitRelOpDefault(RelOp op, Node n) at System.Data.Query.PlanCompiler.PreProcessor.Visit(ProjectOp op, Node n) at System.Data.Query.InternalTrees.ProjectOp.Accept[TResultType](BasicOpVisitorOfT 1 v, noeud N) à System.Data. Query.InternalTrees.BasicOpVisitorOfT 1.VisitNode(Node n) at System.Data.Query.PlanCompiler.PreProcessor.VisitChildren(Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfNode.VisitDefault(Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfNode.VisitPhysicalOpDefault(PhysicalOp op, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT 1.Visit (Opération PhysicalProjectOp, Noeud n) à System.Data.Query.InternalTrees.PhysicalProjectOp.Accept [TResultType] (BasicOpVisitorOfT 1 v, Node n) at System.Data.Query.InternalTrees.BasicOpVisitorOfT 1.VisitNode (Node n) à System.Data.Query.PlanCompiler.PreProcessor.Process() à System.Data.Query.PlanCompiler.PreProcessor.Process (PlanCompiler planCompilerState, StructuredTypeInfo & typeinfo) à System.Data. Query.PlanCompiler.PlanCompiler.Compile (Liste entitySets) à System.Data.Query.PlanCompiler.PlanCompiler.Compile (DbCommandTree ctree, Liste entitySets) à System.Data.EntityClient.EntityCommandDefinition ..cteur (DbProviderFactory storeProviderFactory, DbCommandTree commandTree) InnerException:

Toutes les idées?

+0

Je reçois exactement la même erreur. J'ai utilisé ce modèle pendant un certain temps sans erreur, mais après avoir modifié quelques bases de données et mis à jour le modèle, cette erreur est apparue. La solution est construite sans erreur mais cette erreur est répertoriée dans la liste d'erreurs. Lorsque je lance l'application, j'obtiens l'erreur dès que j'essaie d'accéder au modèle. –

Répondre

1

Je viens juste de me voir arriver ça aussi.

Le problème s'est avéré être une association avec des clés de fin, qui ne faisaient pas toutes partie de la clé primaire pour cette entité!

Par exemple, au plus profond de mon .edmx j'avais cette association:

<AssociationSetMapping Name="Table1Table2" TypeName="MyModel.Table1Table2" StoreEntitySet="Table2"> 
    <EndProperty Name="Table1"> 
     <ScalarProperty Name="Table1Key" ColumnName="Table1Key" /></EndProperty> 
    <EndProperty Name="Table2"> 
     <ScalarProperty Name="Table2Key" ColumnName="Table2Key" /> 
     <ScalarProperty Name="Table2OtherColumn" ColumnName="Table2OtherColumn" /> 
</EndProperty></AssociationSetMapping> 

et la définition de Tableau2 ressemble à ceci:

<EntityType Name="Table2"> 
    <Key> 
     <PropertyRef Name="Table2Key" /> 
    </Key> 
    <Property Name="Table2Key" Type="int" Nullable="false" /> 
    <Property Name="Table2OtherColumn" Type="int" Nullable="false" /> 
    ...other properties... 
</EntityType> 

Notez que Table2OtherColumn est dans le EndProperty, mais ne fait pas partie de la clé réelle. Le supprimer de EndProperty a résolu le problème.

Notez que Entity Framework 4 (Visual Studio 2010) peut détecter ce problème lors de la compilation. Ainsi, si vous avez de sérieux problèmes pour trouver quelle table/propriété est à l'origine, chargez simplement votre projet dans VS 2010 Express (après l'avoir sauvegardé!) Et essayez de compiler.

Questions connexes