Error in One-to-many Relationship in Entity Framework
I have the following tables开发者_如何学JAVA
Users
----------
UserId (int)
Name (varchar)
Stores
----------
StoreId (int)
Name (varchar)
Owner (int) (FK to UserId in Users table)
and the following entities in the edmx file
User{
properties
UserId
Name
}
Store{
properties
StoreId
Name
Owner
navigation proeprties
User
}
I am using a POCO approach. Mapping the edmx file to my POCO entities. Fetching Users works fine, but when I try to create a new user it gets saved in the database successfully but throws the following 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 'FK_Stores_Users_Owner' 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 see you are using EF 4.1 together with EDMX, then you have to declare the Key constrains by yourself either use attributes or Fluent-API. Since EF 4.1 is not compatible with traditional constraint configuration defined in XML.
Furthermore, make sure you use "ADO.NET DbContext generator" rather than "ADO.NET POCO Entity Generator".
精彩评论