久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何創(chuàng)建 LINQ to SQL 事務(wù)?

How to create a LINQ to SQL Transaction?(如何創(chuàng)建 LINQ to SQL 事務(wù)?)
本文介紹了如何創(chuàng)建 LINQ to SQL 事務(wù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一段代碼涉及多個插入,但需要在我完成插入其他表之前執(zhí)行 submitchanges 方法,以便我可以獲取 Id.我一直在網(wǎng)上搜索,但找不到如何在 linq to sql 中創(chuàng)建事務(wù).我已經(jīng)在代碼中我希望交易發(fā)生的地方添加了注釋.

I have a piece of code that involves multiple inserts but need to execute submitchanges method before I finish inserting in other tables so that I can aquire an Id. I have been searching through the internet and couldnt find how to create a transaction in linq to sql. I have put comments in the code where I want the transaction to take place.

    var created = false;
    try
    {
        var newCharacter = new Character();
        newCharacter.characterName = chracterName;
        newCharacter.characterLevel = 1;
        newCharacter.characterExperience = 0;
        newCharacter.userUsername = userUsername;
        newCharacter.characterClassID = ccslst[0].characterClassID;
        //Open transaction


            ydc.Characters.InsertOnSubmit(newCharacter);
            ydc.SubmitChanges();

            foreach (var ccs in ccslst)
            {
                var cs = new CharacterStat();
                cs.statId = ccs.statID;                        
                cs.statValue = ccs.statValue;
                cs.characterID = newCharacter.characterID;
                ydc.CharacterStats.InsertOnSubmit(cs);
            }                    


            var ccblst = ydc.ClassBodies.Where(cb => cb.characterClassID == newCharacter.characterClassID);
            foreach (var ccb in ccblst)
            {
                var charBody = new CharacterBody();
                charBody.bodyId = ccb.bodyId;
                charBody.bodyPartId = ccb.bodyPartId;
                charBody.characterID = newCharacter.characterID;
                ydc.CharacterBodies.InsertOnSubmit(charBody);
            }
            ydc.SubmitChanges();      
            created = true;
        //Commit transaction
        }
        catch (Exception ex)
        {
            created = false;
            //transaction Rollback;                    
        }
        return created;

忘記提到 ydc 是我的數(shù)據(jù)上下文

Forgot to mention that ydc is my datacontext

推薦答案

將整個事情包裹在 TransactionScope 中.在您要提交的位置調(diào)用 transaction.Complete().如果代碼在沒有調(diào)用 Complete() 的情況下退出塊,事務(wù)將被回滾.但是,在查看@s_ruchit 的答案并重新檢查您的代碼后,您可能可以將其重寫為不需要 TransactionScope.第一個示例將 TransactionScope 與您的代碼一起使用.第二個例子做了一些小改動,但實現(xiàn)了相同的目的.

Wrap the whole thing in a TransactionScope. Call transaction.Complete() at the point where you want to commit. If the code exits the block without Complete() being called, the transaction will be rolled back. However, after looking at @s_ruchit's answer and re-examining your code, you could probably rewrite this to not require a TransactionScope. The first example uses the TransactionScope with your code as is. The second example makes some minor changes, but accomplishes the same purpose.

您需要使用 TransactionScope 的地方是當您從數(shù)據(jù)庫中讀取一個值并使用它為正在添加的對象設(shè)置新值時.在這種情況下,LINQ 事務(wù)不會覆蓋第一次讀取,只會覆蓋新值的稍后提交.由于您使用讀取的值來計算寫入的新值,因此您需要將讀取包裝在同一個事務(wù)中,以確保另一個讀取器不會計算相同的值并避免您的更改.在您的情況下,您只是在進行寫入,因此標準 LINQ 事務(wù)應(yīng)該可以工作.

A place where you would need to use the TransactionScope is when you are reading a value from the database and using it to set a new value on an object being added. In this case the LINQ transaction won't cover the first read, just the later submit of the new value. Since you are using the value from the read to calculate a new value for the write, you need the read to be wrapped in the same transaction to ensure that another reader doesn't calculate the same value and obviate your change. In your case you are only doing writes so the standard LINQ transaction should work.

示例 1:

var created = false;

using (var transaction = new TransactionScope())
{
    try
    {
        var newCharacter = new Character();
        newCharacter.characterName = chracterName;
        newCharacter.characterLevel = 1;
        newCharacter.characterExperience = 0;
        newCharacter.userUsername = userUsername;
        newCharacter.characterClassID = ccslst[0].characterClassID;

        ydc.Characters.InsertOnSubmit(newCharacter);
        ydc.SubmitChanges();

        foreach (var ccs in ccslst)
        {
            var cs = new CharacterStat();
            cs.statId = ccs.statID;                        
            cs.statValue = ccs.statValue;
            cs.characterID = newCharacter.characterID;
            ydc.CharacterStats.InsertOnSubmit(cs);
        }                    

        var ccblst = ydc.ClassBodies.Where(cb => cb.characterClassID == newCharacter.characterClassID);
        foreach (var ccb in ccblst)
        {
            var charBody = new CharacterBody();
            charBody.bodyId = ccb.bodyId;
            charBody.bodyPartId = ccb.bodyPartId;
            charBody.characterID = newCharacter.characterID;
            ydc.CharacterBodies.InsertOnSubmit(charBody);
        }
        ydc.SubmitChanges();      
        created = true;

        transaction.Complete();
    }
    catch (Exception ex)
    {
        created = false;
    }
}
return created;

示例 2:

    try
    {
        var newCharacter = new Character();
        newCharacter.characterName = chracterName;
        newCharacter.characterLevel = 1;
        newCharacter.characterExperience = 0;
        newCharacter.userUsername = userUsername;
        newCharacter.characterClassID = ccslst[0].characterClassID;

        ydc.Characters.InsertOnSubmit(newCharacter);

        foreach (var ccs in ccslst)
        {
            var cs = new CharacterStat();
            cs.statId = ccs.statID;                        
            cs.statValue = ccs.statValue;
            newCharacter.CharacterStats.Add(cs);
        }                    

        var ccblst = ydc.ClassBodies.Where(cb => cb.characterClassID == newCharacter.characterClassID);
        foreach (var ccb in ccblst)
        {
            var charBody = new CharacterBody();
            charBody.bodyId = ccb.bodyId;
            charBody.bodyPartId = ccb.bodyPartId;
            newCharacter.CharacterBodies.Add(charBody);
        }
        ydc.SubmitChanges();      
        created = true;
    }
    catch (Exception ex)
    {
        created = false;
    }

這篇關(guān)于如何創(chuàng)建 LINQ to SQL 事務(wù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發(fā)問題)
SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務(wù)級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務(wù)的 SqlCommand 時,我應(yīng)該調(diào)用 Parameters.Clear 嗎?)
Does SqlTransaction need to have Dispose called?(SqlTransaction 是否需要調(diào)用 Dispose?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
主站蜘蛛池模板: 欧美一级全黄 | 黄色片a级| 日本在线视 | 国产精品成人一区二区三区夜夜夜 | 久久久999国产精品 中文字幕在线精品 | 狠狠色香婷婷久久亚洲精品 | 久久久精品综合 | 欧产日产国产精品99 | 欧美一级小视频 | 狠狠av| 久久精品国产久精国产 | 日韩一区中文字幕 | 久久精品亚洲精品国产欧美 | 夜夜撸av | 国产精品色| 欧美福利影院 | 亚洲激情一区二区 | 91一区二区| 中文字幕综合在线 | 国产精品久久久久久久久图文区 | 国产亚洲一区二区三区 | 99国产精品99久久久久久粉嫩 | 不卡av在线 | 九九久久免费视频 | 中文字幕视频网 | 亚洲欧洲在线视频 | 午夜影院在线观看免费 | 久草视频观看 | 欧美网站一区二区 | 91xxx在线观看| 国产精品日韩一区 | av中文在线 | 日本久久久一区二区三区 | 久久精品一区二区三区四区 | 蜜桃一区 | 午夜影视 | 亚洲午夜精品 | 午夜免费精品视频 | 日日爱av | 三级黄色片在线 | 人人鲁人人莫人人爱精品 |