Add Many-to-Many Entity Framework
- by tomcamara
I've got a question:
I have 3 tables:
Users
Menu
UserMenu
UserMenu contains IdMenu and IdUser.
In My DataModel Entity Framework 4.0, I'm filling my User Model and filling User.Menu (Menu is Navigation Properties) with an existing Menu of my table Menu.
I have to save User and Save each related menu item in UserMenu table.
I get the following exception:
The ObjectStateManager does not contain an ObjectStateEntry with a
reference to an object of type 'SGGED.Model.Menu'.
Code
Users objUser = new Users();
objUser.name = itemUsers.name.Trim();
objUser.email = itemUsers.email.Trim();
objUser.password = Util.HashString("12345");
objUser.effdt = DateTime.Now;
objData.Users.AddObject(objUser);
foreach (var itemMenu in itemUsers.Menu)
{
objData.ObjectStateManager.ChangeObjectState(itemMenu, EntityState.Unchanged);
}
affRows = objData.SaveChanges();
Whats the way to handle this issue?
Best Regards,
Miltom Camara