{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/327","repository_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs","labels_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/327/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/327/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/327/events","html_url":"https://github.com/aspnet/EntityFramework.Docs/issues/327","id":200237402,"node_id":"MDU6SXNzdWUyMDAyMzc0MDI=","number":327,"title":"EF6: Simplify transaction handling samples","user":{"login":"rowanmiller","id":4741851,"node_id":"MDQ6VXNlcjQ3NDE4NTE=","avatar_url":"https://avatars2.githubusercontent.com/u/4741851?v=4","gravatar_id":"","url":"https://api.github.com/users/rowanmiller","html_url":"https://github.com/rowanmiller","followers_url":"https://api.github.com/users/rowanmiller/followers","following_url":"https://api.github.com/users/rowanmiller/following{/other_user}","gists_url":"https://api.github.com/users/rowanmiller/gists{/gist_id}","starred_url":"https://api.github.com/users/rowanmiller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rowanmiller/subscriptions","organizations_url":"https://api.github.com/users/rowanmiller/orgs","repos_url":"https://api.github.com/users/rowanmiller/repos","events_url":"https://api.github.com/users/rowanmiller/events{/privacy}","received_events_url":"https://api.github.com/users/rowanmiller/received_events","type":"User","site_admin":false},"labels":[{"id":1522798960,"node_id":"MDU6TGFiZWwxNTIyNzk4OTYw","url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/labels/area-ef6","name":"area-ef6","color":"2353aa","default":false},{"id":1522778246,"node_id":"MDU6TGFiZWwxNTIyNzc4MjQ2","url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/labels/type-enhancement","name":"type-enhancement","color":"bfe5bf","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/milestones/9","html_url":"https://github.com/aspnet/EntityFramework.Docs/milestone/9","labels_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/milestones/9/labels","id":4622776,"node_id":"MDk6TWlsZXN0b25lNDYyMjc3Ng==","number":9,"title":"EF6","description":"","creator":{"login":"ajcvickers","id":1430078,"node_id":"MDQ6VXNlcjE0MzAwNzg=","avatar_url":"https://avatars1.githubusercontent.com/u/1430078?v=4","gravatar_id":"","url":"https://api.github.com/users/ajcvickers","html_url":"https://github.com/ajcvickers","followers_url":"https://api.github.com/users/ajcvickers/followers","following_url":"https://api.github.com/users/ajcvickers/following{/other_user}","gists_url":"https://api.github.com/users/ajcvickers/gists{/gist_id}","starred_url":"https://api.github.com/users/ajcvickers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajcvickers/subscriptions","organizations_url":"https://api.github.com/users/ajcvickers/orgs","repos_url":"https://api.github.com/users/ajcvickers/repos","events_url":"https://api.github.com/users/ajcvickers/events{/privacy}","received_events_url":"https://api.github.com/users/ajcvickers/received_events","type":"User","site_admin":false},"open_issues":42,"closed_issues":1,"state":"open","created_at":"2019-09-02T19:26:48Z","updated_at":"2019-09-06T19:16:09Z","due_on":null,"closed_at":null},"comments":4,"created_at":"2017-01-11T23:24:38Z","updated_at":"2019-09-06T19:16:08Z","closed_at":"2019-09-06T19:16:08Z","author_association":"MEMBER","body":"Our current set of [documentation for using transactions with EF6](https://msdn.microsoft.com/en-us/data/dn456843.aspx) includes some samples that follow this pattern:\r\n\r\n```\r\nusing (var context = new BloggingContext()) \r\n{ \r\n    using (var dbContextTransaction = context.Database.BeginTransaction()) \r\n    { \r\n        try \r\n        { \r\n            DoSomeWork(context);\r\n            context.SaveChanges(); \r\n            dbContextTransaction.Commit(); \r\n        } \r\n        catch (Exception) \r\n        { \r\n            dbContextTransaction.Rollback(); \r\n        } \r\n    } \r\n} \r\n```\r\n\r\nIt turns out that:\r\n1. The explicit call to `dbContextTransaction.Rollback()` is in most cases unnecessary, because disposing the transaction at the end of the using block will take care of rolling back.\r\n2. If an error of sufficient severity occurs in SQL Server, the transaction will get automatically rolled back, and the call to `dbContextTransaction.Rollback()` in the catch block will actually fail.\r\n\r\nFor the second, note that our implementation of `Rollback()` simply delegates to the ADO.NET provider `Rollback()` method so the following note from the remarks in [the underlying method's documentation](https://msdn.microsoft.com/en-us/library/zayx5s0h%28v=vs.110%29.aspx) apply:\r\n\r\n> Try/Catch exception handling should always be used when rolling back a transaction. A Rollback generates an InvalidOperationException if the connection is terminated or if the transaction has already been rolled back on the server.\r\n\r\nThe sample code has proven to be confusing at least for one of our customers and relying on the automatic rollback behavior of disposing without first calling `Commit()` works more reliably and leads to simpler code, so I propose we fix the samples to use that pattern:\r\n\r\n```\r\nusing (var context = new BloggingContext()) \r\n{ \r\n    using (var dbContextTransaction = context.Database.BeginTransaction()) \r\n    { \r\n        DoSomeWork(context);\r\n        context.SaveChanges(); \r\n        dbContextTransaction.Commit(); \r\n    } \r\n} \r\n```\r\n\r\nNote that this pattern applies to EF Core 1.0 as well, when we write the corresponding documentation.\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/comments/528980715","html_url":"https://github.com/aspnet/EntityFramework.Docs/issues/327#issuecomment-528980715","issue_url":"https://api.github.com/repos/aspnet/EntityFramework.Docs/issues/327","id":528980715,"node_id":"MDEyOklzc3VlQ29tbWVudDUyODk4MDcxNQ==","user":{"login":"ajcvickers","id":1430078,"node_id":"MDQ6VXNlcjE0MzAwNzg=","avatar_url":"https://avatars1.githubusercontent.com/u/1430078?v=4","gravatar_id":"","url":"https://api.github.com/users/ajcvickers","html_url":"https://github.com/ajcvickers","followers_url":"https://api.github.com/users/ajcvickers/followers","following_url":"https://api.github.com/users/ajcvickers/following{/other_user}","gists_url":"https://api.github.com/users/ajcvickers/gists{/gist_id}","starred_url":"https://api.github.com/users/ajcvickers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajcvickers/subscriptions","organizations_url":"https://api.github.com/users/ajcvickers/orgs","repos_url":"https://api.github.com/users/ajcvickers/repos","events_url":"https://api.github.com/users/ajcvickers/events{/privacy}","received_events_url":"https://api.github.com/users/ajcvickers/received_events","type":"User","site_admin":false},"created_at":"2019-09-06T19:16:08Z","updated_at":"2019-09-06T19:16:08Z","author_association":"MEMBER","body":"Thank you for requesting a change to the EF6 documentation. We are prioritizing improvements to the EF Core documentation and at this time we are not making changes to the EF6 documentation.\r\n\r\nHowever, we will consider well-written community pull requests to improve the EF6 documentation. The documentation source is located in the [EntityFramework.Docs](https://github.com/aspnet/EntityFramework.Docs) repository on GitHub. To submit a pull request:\r\n* Find the file to update and click on it to open\r\n\r\n![image](https://user-images.githubusercontent.com/1430078/64454137-10199400-d09f-11e9-9d1a-b7fdca2c518e.png)\r\n* Click on the edit button (✏️) to edit in your web browser\r\n\r\n![image](https://user-images.githubusercontent.com/1430078/64454321-85856480-d09f-11e9-85a6-1c93bc6611e2.png)\r\n* When done, add a title and description, then use the \"Create a new branch and start a pull request.\" option.\r\n\r\n![image](https://user-images.githubusercontent.com/1430078/64454455-dac17600-d09f-11e9-922b-0346117011f5.png)\r\n\r\n(Note that you can also clone the docs repo and do a GitHub PR in the normal way.)"}}
{"actor":{"display_login":"ajcvickers"}}