回到目录
这个标题很有意思,关系表插入,就是说主表和外表键在插入时,可能会有同步插的情况,如在建立主表时,扩展表需要同步完成数据的初始化工作,而对于多表插入时,我们为了保证数据的一致性会针它写在事务中,而对于.net中的事件,它在一些情况下,会不那么单纯,对于ef和linq to sql来说,你的事务如果出现多次提交动作(submitchange | savechanges),那么,.net这边会把它提升为分布式事务(MSDTC),即.net认为,对于一个数据表的操作,不会出现多个savechanges,OK,这个可以解释的通,一个数据库,一个提交,这是符合性能要求的,呵呵,但对于我们的架构来说,有时一疏忽,就违背了.net的这个原则,如,我们封装的Insert方法,可能是这样的
public virtual void Insert(TEntity item) { Db.Entry<TEntity>(item); Db.Set<TEntity>().Add(item); this.SaveChanges(); }
这个代码也没有问题,在一个插入动作完成后,系统由SaveChanges方法完成一次提交,正是由于这样的代码,所以我们的麻烦就来了,如果是对两个表的操作吗?
aRepository.Insert(a);
bRepository.Insert(b);
什么问题?数据一致性不能保证,因为没有事务块,呵呵,加上了再看看
using (TransactionScope trans = new TransactionScope()) { aRepository.Insert(a); bRepository.Insert(b); trans.Commit(); }
OK,感觉是没有问题了,但细一想就会看出问题来了,因为我们封装的insert会有提交动作,所以,在这个事务块中,.net会被认识是一个MSDTC(分布式的事务),原因是提交了多次,而如果你没有打开msdtc服务的话,就会出现下面的黄屏了
JSQHKC8VD+J4+G6YeiVVME2mVyEoe2pCH3HU1guz0NQZCmCHjxotAsZGey17ierVl9SbvFfanp+i3S7qSUGTWWeTbt6iHnga2Y5Ktr0uEdmciCZNbUmg65Iv1Ua2irZbB1Ydo0lpPMbUPUhrdekBRKnHfdCS2ZfaDZylQcsm/QIaenUTuyPaWzuvThntMBbPmYFttxr1m1RQdQwHweOkQu5L7neZ7HAvEnC6SOpUWUYIx8cCqhcn9JVg6NShIESsbp9alKypS2LhZn77Sf9apXrad/e5VaQ3xo1CrybwrX8SiKkupNKs9a8jQpS6YOTOsaPFDPK7UahrEZNq7SkkVJO2LaVUjOPue0BaxtJUet3tkCxngYRkpBdStB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7jWU5rIWAhPUeI8rdpMiJHiWPp2t4eyOvJ6r1RI2zUb0nqtq0EDEGENwMbJ/IVa1PfNOtbmaACCIHqqxad1cmZ/NF7myiWJ7atepRhQqe1uvU7S0gCG5grp3I4a2V4KND8SgCRA4ENzzxUzsg2CypnweC4IYmRA4Em6Hw4aBwILhJCJEDQRAES0uIHAiCIFhaQuRAEATB0nJVIudhLyUIgiC4gdm6yFGF8wy0/cZAEARBsEWRg4yB2WzUtnie53k99XaXBATBR5mtiJxUuNVJXb1WSfw+y6soxNktNbwwcDOzXrU2LgiC4DqxXWty9VrFqyS/J0f+jtmobfEqWyoQuc3Oeo9X2dloezFAEHxUuQZrcuaCXP6yXGNnhQay6j1qXGtv1dtSa4h/219HYOuEyIEg2Fb+fzWi/FzCOXz4AAAAAElFTkSuQmCC" alt="" />
注意:系统触发分布式事务的前提是你的WEB服务器与数据库服务器分别部署在两个服务器上,一台不会出现这个问题的。
OK,这个MSDTC是怎么一回事,不是今天要说的重要,如果想学习MSDTC,请看我的这些文章:
第二十六回 将不确定变为确定~transactionscope何时提升为分布式事务?
class="postTitle2">第二十七回 将不确定变为确定~transactionscope何时提升为分布式事务~续
第二十八回 将不确定变为确定~transactionscope何时提升为分布式事务~再续(避免引起不必要的MSDTC)
第二十九回 将不确定变为确定~transactionscope何时提升为分布式事务~大结局
今天我们要说的是在插入关系表时,怎样使它不被提升为分布式事务,事实上,怎么做我们已经知道了,就是方法中只出现一个savechages,呵呵,而对于主表与扩展表来说,对于自增主键的主表,做起来就有些麻烦了,呵呵,我们还需要借助SQL函数来实现主键的获取工作,当主键插入后,通过SQL函数得到新值,然后再为扩展表赋值,最后一些savechange()就可以了,具体我们看一下代码:
int maxID = Convert.ToInt32(new TsingDa_NewLearningBarEntities()//当前表最大ID .Database.SqlQuery<decimal>("SELECT IDENT_CURRENT ('Classroom_Info')") .First()); using (TransactionScope trans = new TransactionScope()) { try { db.Entry<Classroom_Info>(entity); db.Set<Classroom_Info>().Add(entity); //绑定学生 entity.User_Classroom_R.ToList().ForEach(i => { i.ClassroomInfoID = maxID + 1; db.Entry<User_Classroom_R>(i); db.Set<User_Classroom_R>().Add(i); }); db.SaveChanges();//是否为msdtc就看它提交的次数 trans.Complete(); } catch (Exception) { trans.Dispose();//出现异常,事务手动释放 throw; } }
而一次savechanges所产生的SQL也是我们可以接受的,与服务器连接池中开启一个新连接,然后把语句一条一条的发过去,虽然不是一次性发送,但结果我们也是可以接受的,呵呵。
Xeni+PCPmh5h8ySkjtrQlYfYlluH6iEkM0WYN5+itcc7x5ydCYjn7nOde1QkDjzukTG3d2qY0pgKhyYh5KyglMTkBejoj6AhSocFFBTsi3Rf9zbh7ng/r+og/8afmNznjAbKCQGSjWU9tq9Rs3SbF4KzZ22ZEkmtGfzPukXU/qR5hq+9Zpurpbe6+2NMOlqo9tYURsz90NNr6iKdhxKqW7u2sSlgCEQ8yWR8RCpuTMlneLgCZuxzLci4plSHSh0KohIVmAWFTNBSdswYKPS76M3d6YSnu8ozoZtHJyoj4OvfkfEIZLuic/oE/kXq0hVNNFKEGrTAValKmVrcgFq1AaDBizQOKtu2sWV8JgJWJD5tCJBBVZ8ajZBFn/47+1mCUo/zhevTf0wQrEdtiPtcK8bnrjPg9hbtxz5UcEdXzdDOjZzYVx5CNjySuS3rawVKwp4URwROGC+hyBwdbPBkjYrKbpuAqiYCczrEcka440ciXA5sHNZO2ZsqD/8115N/EUElCHCrjcdADqStpaz57aeHseowYpVXT4IQcEZVYbImVIQxny1XIP8llRssP0AV5JR43yMugipURuyAzYnzgsc0BzoJ4x8ss8wEI9ynnyFWBVbhwl9OXw/zpYESxLeJzrXk7Iyba8FsYkftM/BF9jg9HB3M7IwJPhFXTcHcBP8egK3gyRsQ5IgwSaJVzOyOmQOYDe7qH+43SSYg3eagUwmBwLgRvrfXB+yCGXKGt6B1MPFtxYExs2keMkzFzBk8GC4x0cyWcl5FyRIERMw3WbJG6IDIayfwWfEYVKxawmiPWQJNyMluIhJWBuUIz0j39HDELn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