简体   繁体   中英

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?

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.

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.

只需检查它们是否在同一个objectContext中。

您可以通过执行以下代码行来确保您的权限实体为您的对象上下文所知:

objectContext.AddObject("Permissions", permission);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM