EF Mapping and metadata information could not be f

2019-01-14 00:38发布

问题:

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

回答1:

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



回答2:

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();


回答3:

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!)



回答4:

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.


回答5:

I have also seen it when the connection string is not specified in the config file.



回答6:

Another possible issue is, if you are using code-first and your entity type is defined in a separate assembly to where the context is define and you haven't added any custom mappings to the OnModelCreating(DbModelBuild modelBuilder) method, then EF seems to ignore the data annotations.

Solution:

Add modelBuilder.Entity<YourEntityType>(); to the OnModelCreating(DbModelBuild modelBuilder) method.

Related post.



回答7:

Just check the property spelling with the model



回答8:

There might be another reason. I also pulled my hair for a night.

I turned out that there is some disorder in the references in the solution. In my project, the edmx belongs to a project called DataAccess.Dll. But I have no reference to it from my exe. From my exe I have a reference to another project called Business.Dll, and this project has a reference for and old location for the DataAccess.DLL.

Make the following test to see if you have such problem:

  1. Open the bin director of your exe project and keep it visible.
  2. Build the solution.
  3. The first project that will be built is the data access project, and you can see in the bin folder the current time as its modified date.
  4. While other projects are built, you will see that the modified date of the DataAccess project was changed to an old one.

You need to check your references and to ensure they refer to the update locations of your dlls.



回答9:

In my case it was essentialy the same issue, the mapping not being exactly as expected. What happened to me was that I had move a property in a "Table Per Hierarchy inheritance" from one of the subclasses to the base class and forgot to update the model.

I have custom POCO and by copying the edmx file to a blank new project and let it autogenerate the entities, then compare them to what I had helped me in finding the difference.



回答10:

a noob mistake, But I had the error when my access to the DB was with a Read Only username and password. Hope that my mistakes help others.



回答11:

I had a similar problem. I already had one EDMX file for one database using POCO classes and a Context object I wrote myself. When I added a second EDMX for a different database I used the POCO T4 template and then neither EDMX worked and threw the error you mentioned. To resolve it I scrapped my custom POCO and Context and used only the T4 Template and all worked well again.



回答12:

I had a problem where I had added some columns to a table.

In the Table Mappings, I had renamed the column names.

Although I had run 'Transform All Templates' and rebuilt the application, I still got the 'The associated metadata type for type <> contains the following unknown properties or fields <>' error.

The class for this table in Domain.Poco.tt was correct, but I found the corresponding class.Metadata.cs file in Domain.Poco.MetaData.tt had not updated, and had the new columns with the original names - not the new ones I'd specified in Table Mapping.

Solution? I just deleted the offending metadata class, and re-ran 'Transform All Templates' and it was recreated correctly, with the correct column/function names.



回答13:

My problem was I had edited the T4 template to exclude the replication field named "msrepl_tran_version". This caused the database to not match the generated classes which can causes this error message. Just make sure your database and classes match.