What is the meaning of this Entity Framework exception? - c#

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.

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.

Related

Duplicate Record Found Error when Creating Entity

When creating an entity using the Xrm Tooling NuGet package, I receive the following error:
Duplicate Record Found for Entity: [some entity type code] with ID: [some guid]
I am copying a bunch of attributes from another record in the same table (custom entity for which I'm splitting some records out into multiple records as part of a cleanup effort), including the primary field. However, there are no alternate keys defined for this particular entity and no duplicate detection rules enabled either.
Any idea why I'd be receiving this error?
For me personally, this was because I forgot to filter out the primary key attribute from the collection of attributes I was copying to the cloned entity.
Note that typically a better approach would be to setup a reflexive relationship, map attributes through native field mappings, and then use the InitializeFromRequest but this was a quick migration script.
One other thing that could potentially cause this error we've found: if you don't include "RequireNewInstance=true" in your connection string, you may accidentally end up with a cached connection to another org and therefore encounter duplicates if you're performing the same updates against multiple orgs.

Entity Framework 3.5 Not adding all Properties in Model

I'm experiencing an unusual issue with Entity Framework in VS2008. The problem is when I create my Entity Model from my existing database, the designer add all the corresponding tables but it appears that it randomly omits some of my Foreign Key fields. Yet if I browse the Model in the Browser I can clearly see that the fields were included.
This became apparent when during the build I started receiving multiple errors that these fields were not mapped. This led me to manually add the fields in the designer to the corresponding tables and map them to the datasource.
After this however, I get multiple errors:
Error 102 Error 10023: Could not find the conceptual model to validate.
Error 103 Error 10024: Could not find the storage model to validate.
Error 104 Error 10025: Could not find the mapping model to validate.
Error 105 Could not find the Conceptual Schema node to embed as a resource for input file
Error 106 Could not find the Storage Schema node to embed as a resource for input file
Error 107 Could not find the Mapping node to embed as a resource for input file
When I open the edmx file in XML Editor, the file is riddled with squiggly lines.
I have successfully created the Entity Model in VS2010 without issue but our client isn't at 4.0 Framework.
I have identified others having the issue but I can't find any resources to assist on fixing the problem.
Any suggestions would be greatly appreciated.
This is probably EF1 did not support foreign keys. So for foreign key columns EF will create navigation properties but will not create foreign key properties on the Entities. You can find foreign properties in model browser but this is in the model representing the database and not the conceptual model. Note that navigation properties are modeled based on foreign keys in the store and you are not losing the relationship functionality even though you don't have foreign keys exposed. In other words - if you use navigation properties in queries they will be translated correctly to use foreign key values in the SQL queries. One thing where having foreign key properties in the conceptual model is very useful is when you would like to reason or modify a relationship without having to load the related entity - if you know the key value you just set the foreign key property to this value and you are done with it. This is impossible in EF1 - whenever you need to change the relationship or query against a related entity you first need to load said entity and use navigation property.

Entity Framework does not generate association property

So.
I'm having trouble fixing this little problem.
I have several classes, and it's all nice and good. Right up until now. I have now added another class (MatchResult), and it works.
But when I try to make the correct association:
It fails with a runtime exception.
Error:
Schema specified is not valid. Errors:
The relationship 'DbModel.FK_ProductPrice' was not loaded
because the type 'DbModel.Product' is not available.
The following information may be useful in resolving the previous
error:
The required property 'MatchResults' does not exist on the type
'PriceMonitor.Model.Product'.
The relationship 'DbModel.FK_WebshopProduct' was not loaded
because the type 'DbModel.Product' is not available.
The following information may be useful in resolving the previous
error:
The required property 'MatchResults' does not exist on the type
'PriceMonitor.Model.Product'.
It seems - for some reason - that EF does not create the MatchResults property on the Product class.
This approach has worked on every single class I've ever made using EF. Up until now.
I found the answer. Apparently, it is a confirmed bug in the Entity Framework. (WHAT?!)
It seems, if you place the .emdx file in a subfolder, automatic code generation does not work.
See this question.
I just had the same error, the name of my csdl, ssdl and msl were not the same als the name of my edmx and contained the name of a other edmx I had within my solution. I changed the connectionstring and fixed it.
This happens when using EF Database First and the generated POCO classes are not up to date with the edmx file, for example when POCO classes are in a different project than edmx.

Entity Framework: Foreign key with a different type

I've to do an import from an old database (In oracle) a lot of data to an SQL Server database. This import will be done every night, and can be launched from .net application(asp or heavy client)
I'm using the devart connector to the database.
I finished to configure the database connection and started to create an edmx with only data that I'm interessted to.
I encountered a weird thing:
It seems that the foreign key is an Number(10)(translated in int64), and the referenced primary key is a Number(8)(translated in an int32).
I've just no freaking idea why it has be done like this and there is no way that I can ever dream once to change this( :( ).
The problem is that I got errors in my model:
Error 112: The types of all properties in the Dependent Role of a referential
constraint must be the same as the corresponding property types in the Principal
Role. The type of property 'GSM_ID' on entity 'My.NameSpace.Store.GG_IRTSPEZ' does
not match the type of property 'ID' on entity 'My.NameSpace.Store.GG_STAMM' in the
referential constraint 'ORACLE_GIRT_GSM_FK'.
Do you have any idea about how to workaround this problem?

Entity Framework - Inserting entity with multiple models and databases

I have my domain split into multiple Entity Framework models. I have some shared entities that span multiple models (named Lookup), however, these are replaced with "using" references using the methods described in Working With Large Models In Entity Framework. However, what makes my case slightly more unique is that I'm also separating these models into multiple databases (one per model).
I'm having a problem inserting one of my shared entities into my common DB. It's failing with the error:
The member with identity
'Harmony.Members.FK_ResidentialAddress_ResidenceTypeLookup'
does not exist in the metadata
collection.
That foreign key that it's referring to does not exist on the "common DB". But I'm also not working with the entity on the other side of the relationship (named ResidentialAddress); nor do I even have the context that would contain the other entity initialized (named MembersDb). However, both models are compiled into the same assembly.
There are no navigation properties going from Lookup to ResidentialAddress. Though there is a navigation property in the other direction (which I won't be persisting - only using in memory).
My MetadataWorkspace for the EntityConnection of the CommonDb context was explicitly initialized with only the SSDL/CSDL/MSL for the data required for that database. I have confirmed there is no references to the foreign key mentioned in that set of schema data.
var metaAssembly = typeof(CommonDb).Assembly;
var schemaResources = new string[]
{
String.Format("res://{0}/Common.ssdl", metaAssembly.FullName),
String.Format("res://{0}/Common.csdl", metaAssembly.FullName),
String.Format("res://{0}/Common.mdl", metaAssembly.FullName),
}
MetadataWorkspace metadata = new MetadataWorkspace(schemaResources, new []{ metaAssembly });
EntityConnection connection = new EntityConnection(metadata, myDatabaseConnection);
POSSIBLE CLUE: It does work when I go into the generated classes and remove all of the EdmRelationshipAttribute attributes along with their paired EdmRelationshipNavigationPropertyAttribute from the related models (MembersDb).
Key questions:
So why is it that Entity Framework is trying to do something with the relationship that is for an entity that is neither in scope and nor will it be affected by the insertion of the record!?
I am happy to have the generated code remove the attributes mentioned above, but I still want the navigation properties to remain. How would I go about altering the CSDL to achieve that?
NOTE: Persistence of the "child" models is not a priority, nor is the integrity of their now cross-DB foreign keys. These databases are persisted using SQL CE but they were originally generated from a single master SQL Server database.
If each part of your model is written to a separate database, then perhaps the edmx files should not know about each other (about entities or relationship to entities that do not belong to them).
How about trying one of the following approaches:
(To end up with same entities classes for each part, but make EF oblivious of connections between them.)
Remove the "usings" from edmx + cancel auto generation and create classes yourself.
Remove the "usings" from edmx + modify t4 template to read more than one edmx when creating the classes.
Copy edmx files aside so you have two sets of edmxs.
3.a. Use set #1 for auto generation of entities.
3.b. Modify set #2 by removing the "usings" and use for generation of repository classes (objectsets).
Let me know if one of these works.
Good luck,
Danny.

Categories