Add Many-to-Many Entity Framework
        Posted  
        
            by 
                tomcamara
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by tomcamara
        
        
        
        Published on 2012-06-16T02:38:20Z
        Indexed on 
            2012/06/16
            3:16 UTC
        
        
        Read the original article
        Hit count: 206
        
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
© Stack Overflow or respective owner