I’m having a bit of an… odd issue. I’m getting an error with saving a newly created entity that has a * to 1 relation with another entity. Bit difficult to explain but here’s the set up:
(Note: I am using MVC 2 – Entity Framework 4)
My entities are set up as follows (names replaced for readability, but the structure is as described):
There are some managers in our company. (tblManagers) Each manager owns an undefined amount of Stores (tblStores). Each store has an undefined amount of Equipment (tblEquipment).
So my DataModel is as follows:
(tblManagers) 1<—–>* (tblStores) 1<——>* (tblEquipment)
This setup is valid, as we already have a lot of data in there (from exporting an old Access file).
When creating a new equipment, you obviously have to select which store it belongs to. The value selected in the dropdownlist (the ID of the Store) is passed to the HttpPost method, which executes the following code:
db is my database context variable
myNewItem.tblStore = db.tblStores.Single(x => x.Id == store_id_from_dropdown);
db.tblEquipments.AddObject(myNewItem);
db.SaveChanges();
Which seems to me to be valid code. Resharper sees no issues, also no compile errors (or even warnings). However, when running the code, I encouter the following exception (thrown on db.SaveChanges()):
Entities in 'CMT_DevEntities.tbl_Stores' participate
in the 'tblManagerstblStores' relationship.
0 related 'tblManager' were found.
1 'tblManager' is expected.
I have no idea why it’s even looking at the tblManager as there is no reference to it in my code. I understand that this has to do with the relationship between managers and their stores, but I dont see why.
Here’s the part that confuses me:
- When looking at the debugvalues of
myNewItem, I can see the store is added to the entity. I can even check the tblManager property of the Store, and it is the correct tblManager. So this confirms everything is OK in the database (Stores couldn’t even exist if they didn’t have a Manager). - I’ve actually left out a bit of the code. There is an exactly similar navigation property in the Equipment (let’s say Supplier. The Supplier in turn has navigation property to a Country. The relations are exactly the same). I get no errors for this one though. Only the Store/Manager one.
I’m probably missing something, maybe something quite simple. Either way, I have no idea why this is happening.
So my question is: Given an entity created by the ModelBinder in MVC and an (int) ID for the navigation property, how do I add my new entity to the database?
Instead of loading all the
Storeentities, you can add a Foreign Key Id forStoreto yourEquipmententity (equipment.StoreId).This will make sure that EF sees the relation between your newly created
Equipmentand yourStoreentity without loading the whole graph.Since you are using the Model first approach, you can find the documentation on how to do this here on MSDN.
The important part is in the Note sections:
You should check the ‘Add foreign key properties to the ‘Equipment’ Entity’ in the Add Association dialog.
Using Foreign Key properties is a new feature in Entity Framework 4. It will make your live a lot easier.
Here you can find a more detailed explanation.