---
title: "Entity framework inserting a many-to-many relationship between two existing objects while updating"  
description: "Entity framework inserting a many-to-many relationship between two existing objects while updating"  
author: "Anonymous User"  
published: 2013-09-28  
updated: 2013-09-28  
canonical: https://www.mindstick.com/forum/1546/entity-framework-inserting-a-many-to-many-relationship-between-two-existing-objects-while-updating  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 1 minute  

---

# Entity framework inserting a many-to-many relationship between two existing objects while updating

I'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to do this:

\

```
using(var context = new SampleEntities())
{
     User user = select a user from database;

     //Update user's properties
     user.Username = ...
     user.Website = ...

     //Add a role
     Role role = select a role from database
     //trying to insert into table UserRoles which has columns (UserID, RoleID)
     user.Roles.Add(role);

     //Apply property changes
     context.ApplyPropertyChanges("Users", user);
     context.SaveChanges();
}
```

However, I get an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [telling me](https://answers.mindstick.com/qa/50024/my-computer-is-telling-me-my-startup-drive-is-nearly-full-how-to-i-make-room) that "**The existing object in the [ObjectContext](https://www.mindstick.com/interview/34244/explain-the-difference-between-dbcontext-and-objectcontext) is in the Added state**" and can't "ApplyPropertyChanges". If "ApplyPropertyChanges()" is removed, it adds a [User](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server).

What [orders](https://answers.mindstick.com/qa/43206/what-were-the-fundamental-orders-of-connecticut-and-who-wrote-them) should these [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) be called? I don't need to do them separately [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not)?

Thanks.

## Replies

### Reply by Anonymous User

Hey Chintoo!

It doesn't look to me like your User object is detached, so you shouldn't need to call ApplyPropertyChanges().


---

Original Source: https://www.mindstick.com/forum/1546/entity-framework-inserting-a-many-to-many-relationship-between-two-existing-objects-while-updating

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
