Mato
Mato

Reputation: 830

Entity Framework won't save relations

I have the following situation:

role.Permissions.Add(permission);
objectContext.SaveChanges();

When I now take a look in the relations table Roles_Permissions the newly added permission to the role is not present. It only saves the new relation when I dispose the object context. Am I doing something wrong or does a call to SaveChanges doesn't save changes on relationship sets?

Upvotes: 3

Views: 398

Answers (4)

Ryan Gamal
Ryan Gamal

Reputation: 63

You can ensure that your permission entity is known to your object context by executing the following line of code:

objectContext.AddObject("Permissions", permission);

Upvotes: 0

Isaac Abraham
Isaac Abraham

Reputation: 3502

Check the ObjectStateManager - before calling SaveChanges, have a look on the object state manager to get all changes that have a state of Added - it should return just the one item which is that permission object. If it's not in there, something else is wrong. Calling SaveChanges should persist your changes then and there.

Upvotes: 1

franklins
franklins

Reputation: 3738

Just check whether they are in the same objectContext.

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

It sounds like you are using a transaction.

The changes made are not visible outside the transaction scope untill the transaction is committed.

And the transaction scope is not being committed until the object context is disposed.

Upvotes: 0

Related Questions