我在一个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);
}
}
}
}
执行时,它会在
上引发错误 带有内部异常消息的 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.
我需要在单个事务中更新两个不同的数据库。 怎么做?
答案 0 :(得分:3)
我认为这是一个MySQL限制,因为只有XA事务支持分布式事务(参与全局事务的多个独立事务资源)。
据我所知,MySql .net连接器确实支持分布式事务。尝试在连接字符串中设置AutoEnlist=false
。