0

What is the meaning of this exception? Where can I find references of the cause to this kind of exception?

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Metadata information for the relationship 'MyModel.FK_T_WORKER_VEHICLE_T_VEHICLE' could not be retrieved. If mapping attributes are used, make sure that the EdmRelationshipAttribute for the relationship has been defined in the assembly. When using convention-based mapping, metadata information for relationships between detached entities cannot be determined. Parameter name: relationshipName

I am using edmx with code generation set to Default. The pocos are in different project then the edmx file.

Naor
  • 23,465
  • 48
  • 152
  • 268
  • 2
    I think it means your edmx or generated code is busted. See this question for a possible solution: http://stackoverflow.com/questions/2350514/ef-mapping-and-metadata-information-could-not-be-found-for-entitytype-error – Merlyn Morgan-Graham Oct 20 '11 at 18:39
  • @Merlyn Morgan-Graham: What do you mean by "Busted"? I looked on the link but nothing there helped.. do you have any idea what can I do in order to solve this issue? – Naor Oct 23 '11 at 20:50
  • "busted" means "broken" or "non-functional". – Michael Petrotta Oct 23 '11 at 22:32
  • @Michael Petrotta: Where can I find the problem?.. How can I test it? – Naor Oct 23 '11 at 22:37
  • How are you accomplishing POCOs in a different project *and* code generation set to default? Those sound mutually exclusive to me, and sound like a possible source of errors in keeping the edmx, entities, and DB in sync. When I said it is busted, I mean your objects, edmx, and or DB are out of sync with each other. Search through one or all of them by reading the error message - look for the objects that define that foreign-key relationship. – Merlyn Morgan-Graham Oct 24 '11 at 18:43
  • @Merlyn Morgan-Graham: You know, I am looking in my code again and again. In the application_start (global.asax) I inject MyAppEntities (the context) which generated automatically by the edmx and I really don't know how the entity framework knows to use my pocos.. And it works.. the EF returns allways my objects.. (in addition to this question: http://stackoverflow.com/questions/7839682/how-to-configure-entity-framework-poco-with-similar-names). Any idea?.. – Naor Oct 24 '11 at 22:46
  • @Merlyn Morgan-Graham: Ok. I think I understand now. Look in my Repository class (you can find it here http://codereview.stackexchange.com/questions/5556/entity-framework-with-repository-and-unit-of-work-pattern-and-poco-architecture under MyApp.Data.EF4 section). There is a property called 'ObjectSet' and it use: '_objectSet = this.Context.CreateObjectSet();'. When I instanciate the repository with my poco, it create objectset in type of my poco and not the generated code. What do you think? – Naor Oct 24 '11 at 22:50
  • @Naor: I think you should either ditch your POCOs and use the generated code (which should have the correct attributes), or ditch the EDMX and go purely code-first. Mixing the two seems like it could be made to work, but would not be well supported, and would give you a lot of headache for little to no benefit. There's some videos on the front page of the EF site demoing the code-first option: http://msdn.microsoft.com/en-us/data/aa937723 – Merlyn Morgan-Graham Oct 24 '11 at 23:08
  • @Merlyn Morgan-Graham: Thanks for the suggestion but this is a project that already running with database and data. I prefer my pocos because they represent my business logic and I added there more abilities. If I understand correct, I actually don't use the abilities of the Context that the designer generated for me.. Am I right?.. – Naor Oct 25 '11 at 07:57
  • @Merlyn Morgan-Graham: You know, I set the Code Generation Strategy to None and added an empty class called "test" that inherit from ObjectContext. And the application worked. I even don't need the generated code. Unfortunately this exception continues showing up. – Naor Oct 25 '11 at 18:50

1 Answers1

0

In my case, I got same error when I added a new entity(A) to my EDMX diagram which A has Foreign key reference to an existing entity B. Then this error came up when I tried to do CRUD for B. I did some researches and followed a tip on MetadataException when using Entity Framework Entity Connection which advises to rename metadata in Connection String but it didn't work for me.

Steps to resolve: The issue was in entity A not B. You need to check newly added entity A to be mapped correctly to your class A in your domain. Check property names and property types to be same (i.e. a column might need to be converted to an Enum type in your diagram to be same with your entity). Keep in mind that property names are case sensitive so if they are in upper-case in your entity it should be in upper-case in your diagram.

Community
  • 1
  • 1
Amir Chatrbahr
  • 2,260
  • 21
  • 31
  • yeah that makes sense after 3 years :) just thought it could be useful for whoever search this issue and come across to this page. – Amir Chatrbahr Dec 11 '14 at 22:26