Argument exception after trying to use TryGetObjectByKey

Posted by Rickjaah on Stack Overflow See other posts from Stack Overflow or by Rickjaah
Published on 2010-04-26T12:51:28Z Indexed on 2010/04/26 12:53 UTC
Read the original article Hit count: 710

Hi,

I'm trying to retrieve an object from my database using entity (framework 4) When I use the following code it gives an ArgumentException: An item with the same key has already been added.

    if (databaseContext.TryGetObjectByKey(entityKey, out result))
    {
        return (result != null && result is TEntityObject) ? result as TEntityObject : null;
    }
    else
    {
        return null;
    }

When I check the objectContext, I see the entities, but only if I enumerate the specific list of entities manually using VS2010, it works.

What am I missing? Do I have to do something else before i can get the item from the database? I have lazy loading set to true.

I searched google, but could not find any results, the same for the msdn library

© Stack Overflow or respective owner

Argument exception after trying to use TryGetObjectByKey

Posted by Rickjaah on Stack Overflow See other posts from Stack Overflow or by Rickjaah
Published on 2010-04-23T15:27:02Z Indexed on 2010/04/26 13:53 UTC
Read the original article Hit count: 710

Hi,

EDIT: Somethings wrong.... I have to use objectContext.Frontpages.ToArray() before I can use TryGetObjectByEntityKey(). Any ideas anyone?

I'm trying to retrieve an object from my database using entity (framework 4) When I use the following code it gives an ArgumentException: An item with the same key has already been added.

        if (databaseContext.TryGetObjectByKey(entityKey, out result))
        {
            return (result != null && result is TEntityObject) ? result as TEntityObject : null;
        }
        else
        {
            return null;
        }

When I check the objectContext, I see the entities, but only if I enumerate the specific list of entities manually using VS2010, it works.

What am I missing? Do I have to do something else before i can get the item from the database?

I searched google, but could not find any results, the same for the msdn library

EDIT: Still working on this.... It's a weird problem. I retrieve a value, but get an error that says a duplicate item exists.

STACKTRACE:

[ArgumentException: An item with the same key has already been added.] System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52 System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) +9549131 System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.LoadRelationshipTypes() +661 System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.LoadTypesFromAssembly() +17 System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load() +25 System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.Load() +4 System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData) +160 System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action1 logLoadMessage, Object& loaderCookie, Dictionary2& typesInLoading, List1& errors) +166 System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage) +316 System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadAssemblyForType(Type type, Assembly callingAssembly) +306 System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadFromEntityType(EntityType type, Assembly callingAssembly) +109 System.Data.Objects.ObjectContext.TryGetObjectByKey(EntityKey key, Object& value) +288

EDIT: Lazy loading is set to true.

EDIT: Somethings wrong.... I have to use objectContext.Frontpages.ToArray() before I can use TryGetObjectByEntityKey(). Any ideas anyone?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about entity-framework-4