[Solved] An exception of type ‘System.Data.Entity.Core.MetadataException’ occurred in EntityFramework.dll but was not handled in user code

The issue “System.Data.Entity.Core.MetadataException” and “Unable to load the specified metadata resource” is the most common error, but there is a simple way to fix this issue in most cases.

In this article, I am going to give the simplest way to solve this error. But this solution may work in some cases and may not work in some cases. As its a most common issue in .Net and especially when running a project in Visual Studio.

Problem

When we try to run the NET application project in Visual Studio, when building the application give error like An exception of type ‘System.Data.Entity.Core.MetadataException’ occurred in EntityFramework.dll but was not handled in user code as shown in the below screenshot:

Sample exception:

An exception of type ‘System.Data.Entity.Core.MetadataException’ occurred in EntityFramework.dll but was not handled in user code

Additional information: Unable to load the specified metadata resource.

at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources(String assemblyName, String resourceName, ICollection1 uriRegistry, MetadataArtifactAssemblyResolver resolver) at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.CreateResourceLoader(String path, ExtensionCheck extensionCheck, String validExtension, ICollection1 uriRegistry, MetadataArtifactAssemblyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoader.Create(String path, ExtensionCheck extensionCheck, String validExtension, ICollection1 uriRegistry, MetadataArtifactAssemblyResolver resolver) at System.Data.Entity.Core.Metadata.Edm.MetadataCache.SplitPaths(String paths) at System.Data.Entity.Core.Common.Utils.Memoizer2.<>c__DisplayClass2.b__0()
at System.Data.Entity.Core.Common.Utils.Memoizer2.Result.GetValue() at System.Data.Entity.Core.Common.Utils.Memoizer2.Evaluate(TArg arg)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetArtifactLoader(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetMetadataWorkspace(DbConnectionOptions effectiveConnectionOptions)
at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace()
at System.Data.Entity.Core.Objects.ObjectContext.RetrieveMetadataWorkspaceFromConnection()
at System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory)
at System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at agCapture.Data.Repository.DTO.ExecutionContextInitializer.<>c__DisplayClass1_0.b__1() in Data.Repository\DTO\ExecutionContextInitializer.cs:line 43
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()

{System.Collections.Generic.List1[System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderResource] LoadResources(System.String, System.String, System.Collections.Generic.ICollection1[System.String], System.Data.Entity.Core.Metadata.Edm.MetadataArtifactAssemblyResolver)}

Solution

The solution for the above issue is very simple in my case, I had to delete the obj folder which will be present in the root folder of the visual studio project (in windows folders not within the visual studio tool). Then the error System.Data.Entity.Core.MetadataException was solved automatically. We don’t need to analyze more here as this issue is mostly related to visual studio and hopefully Microsoft will resolve this issue with the latest visual studio.

This generally occurs when we take source code for the first time from the repository. The obj folder which would be generated by Visual Studio for others may cause a problem to us when we take source code from the repository. So we may need to clear it off before building the project.

The above solution is like a first and easy way to fix the issue if nothing wrong with the application code. If the above solution did not work for you, please try to resolve the issue in the code (maybe compiled errors).

Conclusion

The issue System.Data.Entity.Core.MetadataException looks so confusing and most common, but if there are no real problems with the application code, then deleting the obj folder should solve the issue. Otherwise, it must be compiled time issue.

Default image
Sreenivasa Rangan TR

Software professional with 7+ years of experience in all phases of software development with robust problem-solving skills and proven experience in creating and designing software in a test-driven environment.

Leave a Reply