Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Mapping and metadata information could not be found for EntityType Error

I have encountered an exception when I use Entity Framework 4.0 RC. My Entity Framework model is encapsulated in a private assembly who's name is Procurement.EFDataProvider and my POCO classes are inside of another assembly Procurement.Core The relation between Core(Business Logic)and EFDataProvider(Data Access) is with a factory named DataProvider

so when I try to create an objectset

objectSet = ObjectContext.CreateObjectSet<TEntity>();

I get an error:

Mapping and metadata information could not be found for EntityType 'Procurement.Core.Entities.OrganizationChart'.

like image 686
Reza Zareian Avatar asked Feb 28 '10 08:02

Reza Zareian


4 Answers

For anyone else dealing with the error, I think it's worth mentioning some scenarios that I've found that cause this (extremely unhelpful) error:

  • Misspelled properties (case-sensitive!)
  • Properties missing in the POCO class
  • Type mismatches between the POCO and entity-type (e.g., int instead of long)
  • Enums in the POCO (EF doesn't support enums right now as I understand)

There might be other causes as well.

HTH

like image 86
joniba Avatar answered Oct 20 '22 01:10

joniba


This is probably because EF can't find the embedded mapping information. Inside your connection string you'll probably have something like his:

metadata=res://*/Models.MyModels.csdl|...etc

That * is a wildcard, telling the object context to try and find the embedded mapping information from, I think, scanning all the loaded assemblies. If the assembly isn't loaded, EF won't find it.

What you need to do is provide the connection string with more information about where your mapping information is embedded. Change the * to the specific assembly name of your mapping code:

metadata=res://Procurement.EFDataProvider/Models.MyModels.csdl

If that fails, find the Assembly and directly load it into your ObjectContext using:

ObjectContext.Metadataworkspace.LoadFromAssembly();
like image 26
John Farrell Avatar answered Oct 20 '22 02:10

John Farrell


Not directly related to the above, but if you get this error message and you have mixed a POCO and a regular model: bad idea!

See also the comment from JRoppert at EF4 POCO (not using T4): Mapping and metadata information could not be found for EntityType (thanks JRoppert!)

like image 3
Roy de Boer Avatar answered Oct 20 '22 01:10

Roy de Boer


I was getting this error because I had more than edmx file in the same assembly with out proper use of custom namespaces.

Here is what is said about the exception in System.Data.Objects.ObjectContext

// Exceptions:

    //   System.InvalidOperationException:
    //     When the System.Data.Metadata.Edm.EntitySet from entitySetName
    //     does not match the System.Data.Metadata.Edm.EntitySet of the object’s
    //     System.Data.EntityKey.
    // -or-
    //     When the System.Data.Objects.ObjectContext.DefaultContainerName
    //     property is not set on the System.Data.Objects.ObjectContext and 
    //     the name is not qualified as part of the entitySetName parameter.
    // -or-
    //     When the specified type belongs to more than one entity set.
like image 2
Ken Avatar answered Oct 20 '22 01:10

Ken