{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Identity/issues/739","repository_url":"https://api.github.com/repos/aspnet/Identity","labels_url":"https://api.github.com/repos/aspnet/Identity/issues/739/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Identity/issues/739/comments","events_url":"https://api.github.com/repos/aspnet/Identity/issues/739/events","html_url":"https://github.com/aspnet/Identity/issues/739","id":132297864,"number":739,"title":"Multiple operations in a single transaction is broken.","user":{"login":"joshmouch","id":3098820,"avatar_url":"https://avatars.githubusercontent.com/u/3098820?v=3","gravatar_id":"","url":"https://api.github.com/users/joshmouch","html_url":"https://github.com/joshmouch","followers_url":"https://api.github.com/users/joshmouch/followers","following_url":"https://api.github.com/users/joshmouch/following{/other_user}","gists_url":"https://api.github.com/users/joshmouch/gists{/gist_id}","starred_url":"https://api.github.com/users/joshmouch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshmouch/subscriptions","organizations_url":"https://api.github.com/users/joshmouch/orgs","repos_url":"https://api.github.com/users/joshmouch/repos","events_url":"https://api.github.com/users/joshmouch/events{/privacy}","received_events_url":"https://api.github.com/users/joshmouch/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/Identity/labels/bug","name":"bug","color":"fc2929"}],"state":"open","locked":false,"assignee":{"login":"HaoK","id":6537861,"avatar_url":"https://avatars.githubusercontent.com/u/6537861?v=3","gravatar_id":"","url":"https://api.github.com/users/HaoK","html_url":"https://github.com/HaoK","followers_url":"https://api.github.com/users/HaoK/followers","following_url":"https://api.github.com/users/HaoK/following{/other_user}","gists_url":"https://api.github.com/users/HaoK/gists{/gist_id}","starred_url":"https://api.github.com/users/HaoK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HaoK/subscriptions","organizations_url":"https://api.github.com/users/HaoK/orgs","repos_url":"https://api.github.com/users/HaoK/repos","events_url":"https://api.github.com/users/HaoK/events{/privacy}","received_events_url":"https://api.github.com/users/HaoK/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/aspnet/Identity/milestones/19","html_url":"https://github.com/aspnet/Identity/milestones/1.0.0-rc2","labels_url":"https://api.github.com/repos/aspnet/Identity/milestones/19/labels","id":1369200,"number":19,"title":"1.0.0-rc2","description":null,"creator":{"login":"rustd","id":1132652,"avatar_url":"https://avatars.githubusercontent.com/u/1132652?v=3","gravatar_id":"","url":"https://api.github.com/users/rustd","html_url":"https://github.com/rustd","followers_url":"https://api.github.com/users/rustd/followers","following_url":"https://api.github.com/users/rustd/following{/other_user}","gists_url":"https://api.github.com/users/rustd/gists{/gist_id}","starred_url":"https://api.github.com/users/rustd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rustd/subscriptions","organizations_url":"https://api.github.com/users/rustd/orgs","repos_url":"https://api.github.com/users/rustd/repos","events_url":"https://api.github.com/users/rustd/events{/privacy}","received_events_url":"https://api.github.com/users/rustd/received_events","type":"User","site_admin":false},"open_issues":11,"closed_issues":31,"state":"open","created_at":"2015-10-21T21:38:11Z","updated_at":"2016-02-17T22:12:33Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2016-02-09T00:31:54Z","updated_at":"2016-02-19T20:45:17Z","closed_at":null,"body":"It is currently impossible (or at least very difficult) to 1. create a user, 2. add the user to a role, and 3. perform some custom actions all within one transaction using the built-in classes.\r\n\r\nI'm almost there by doing the following:\r\n1. Set UserStore.AutoSaveChanges = false \r\n2. Do multiple operations on user\r\n3. Call DataContext.SaveChanges.\r\n\r\nHowever, the problem is:\r\nThe process of creating a user calls UserStore.CreateAsync(), which adds the user to the context like so:\r\n````\r\nthis.Context.Add<TUser>(user, GraphBehavior.IncludeDependents);\r\n````\r\n\r\nBut then the process of adding a user role calls UserStore.UpdateAsync(), which changes the user's state in the context by running this line:\r\n\r\n````\r\nthis.Context.Update<TUser>(user, GraphBehavior.IncludeDependents);\r\n````\r\n\r\nSo then when you go to save changes, you get a SQL error because it's trying to UPDATE the user rather than INSERT the user (it actually tries to INSERT the UserRole first, because of dependencies)."},"comment":{"url":"https://api.github.com/repos/aspnet/Identity/issues/comments/186398151","html_url":"https://github.com/aspnet/Identity/issues/739#issuecomment-186398151","issue_url":"https://api.github.com/repos/aspnet/Identity/issues/739","id":186398151,"user":{"login":"HaoK","id":6537861,"avatar_url":"https://avatars.githubusercontent.com/u/6537861?v=3","gravatar_id":"","url":"https://api.github.com/users/HaoK","html_url":"https://github.com/HaoK","followers_url":"https://api.github.com/users/HaoK/followers","following_url":"https://api.github.com/users/HaoK/following{/other_user}","gists_url":"https://api.github.com/users/HaoK/gists{/gist_id}","starred_url":"https://api.github.com/users/HaoK/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HaoK/subscriptions","organizations_url":"https://api.github.com/users/HaoK/orgs","repos_url":"https://api.github.com/users/HaoK/repos","events_url":"https://api.github.com/users/HaoK/events{/privacy}","received_events_url":"https://api.github.com/users/HaoK/received_events","type":"User","site_admin":false},"created_at":"2016-02-19T20:45:17Z","updated_at":"2016-02-19T20:45:17Z","body":"Are you using the user manager APIs and setting AutoSaveChanges to false?  Basically if you turn off AutoSaveChanges the expectation is that you are probably not going to want to use the UserManager APIs, since those are implemented basically by calling some set method on the store, and then update on the user which explicitly save changes.  \r\n\r\nTypically you would want to write your own extensions that do not call user.update until you are ready.\r\n\r\n"}}