[C#.NET][Entity Framework] TransactionScope @ EF6
續上篇,http://www.dotblogs.com.tw/yc421206/archive/2014/03/20/144457.aspx 交易處理 還可以使用 TransactionScope ,需要交易確保,只要調用 TransactionScope.Complete 方法 即可,這同等 DbContextTransaction.Commit 方法
在實作時碰到了兩個問題
MSDTC 未啟動,沒意外的話手動啟用它就可以了
若有意外,請考以下
http://www.dotblogs.com.tw/ccc/archive/2012/03/25/71022.aspx
MySQL 提供的元件不支援 Distributed Transaction
可能會跳出
{"MySQL Connector/Net does not currently support distributed transactions."}
或是無法寫入資料,調用 SaveChanges 得到 0
在這裡我使用 Sql 2012 LocalDB 演練,這跟上篇情境相同,
{ using (var localDbContext1 = new LocalDbContext("localDb1")) using (var localDbContext2 = new LocalDbContext("localDb2")) { localDbContext1.Members.Load(); localDbContext2.Members.Load(); using (var transactionScope = new TransactionScope()) { localDbContext1.Members.Add(new Member() { Id = 1, Name = "余小章" }); localDbContext1.Members.Add(new Member() { Id = 1, Name = "余小章" }); localDbContext2.Members.Add(new Member() { Id = 1, Name = "余小章" }); try { var localResult1 = localDbContext1.SaveChanges(); var localResult2 = localDbContext1.SaveChanges(); if (localResult1 == 0 || localResult2 == 0) { throw new DbUpdateException(); } transactionScope.Complete(); MessageBox.Show("OK"); } catch (DbUpdateException) { MessageBox.Show("Rollback"); } catch (Exception ex) { //TODO:例外處理 } } } }
完整程式碼
{ public DbSet<Member> Members { get; set; } public LocalDbContext(string connectString) : base(connectString) { } public LocalDbContext() : base("localDb") { } }
組態檔
<configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <system.data> </system.data> <connectionStrings> <add name="mySql" providerName="MySql.Data.MySqlClient" connectionString="server=localhost; user id=root; password=pass@w0rd1~;database=demo; port=3306" /> <add name="localDb" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=demo;Integrated Security=True" /> <add name="localDb1" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=demo1;Integrated Security=True" /> <add name="localDb2" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=demo2;Integrated Security=True" /> </connectionStrings> </configuration>
本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/03/20/144458.aspx
參考文章:http://msdn.microsoft.com/en-us/data/dn456843
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET