Raj*_*mar 2 c# mysql entity-framework transactionscope
我写HV以下功能having two database context于一体transaction scope。我使用MySql与EF 5.0
private static void SyncPremiumStores(JoyRydeWebPortalData.joyryde_WebPortalEntities contextWebPortal, JoyRydeMallStoreData.joyryde_MallStoreEntities contextMallStore)
{
using (TransactionScope scope = new TransactionScope())
{
foreach (var objWebPortalPremiumStore in contextWebPortal.tbl_premium_store.Where(x => x.INT_DATA_TRANS_STATUS == 0).ToList())
{
try
{
var objMallStore = contextMallStore.tbl_store.Where(x => x.LNG_STORE_ID == objWebPortalPremiumStore.LNG_STORE_ID).FirstOrDefault();
if (objMallStore != null)
{
JoyRydeMallStoreData.tbl_premium_store objMallPremiumStore = new JoyRydeMallStoreData.tbl_premium_store()
{
DAT_CREATED = objWebPortalPremiumStore.DAT_CREATED,
DAT_PREMIUM_FROM = objWebPortalPremiumStore.DAT_PREMIUM_FROM,
DAT_PREMIUM_TO = objWebPortalPremiumStore.DAT_PREMIUM_TO,
LNG_PRIMARY_STORE_ID = objMallStore.LNG_PRIMARY_STORE_ID,
LNG_STORE_ID = objMallStore.LNG_STORE_ID,
TXT_PACK_NAME = ""
};
contextMallStore.tbl_premium_store.Add(objMallPremiumStore);
objWebPortalPremiumStore.INT_DATA_TRANS_STATUS = 1;
}
contextMallStore.SaveChanges();
contextWebPortal.SaveChanges();
scope.Complete();
}
catch (Exception ex)
{
LogUtility.WriteErrorLog(ex);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在执行时,它会引发我的错误
var objMallStore = contextMallStore.tbl_store.Where(x => x.LNG_STORE_ID == objWebPortalPremiumStore.LNG_STORE_ID).FirstOrDefault(); 带有内部异常消息的行
Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.
我需要在单个事务中更新两个不同的数据库。怎么办??
小智 5
我相信这是 MySQL 的限制,因为只有 XA 事务支持分布式事务(参与全局事务的多个独立事务资源)。
据我所知,MySql .net 连接器确实支持分布式事务。尝试AutoEnlist=false在连接字符串中设置。