簡體   English   中英

LINQ SQL查詢,SubmitChanges未將更改提交到數據庫

[英]LINQ SQL query, SubmitChanges is not submitting changes to DB

我正在編寫LINQpad腳本來匿名化存儲在表的XML列中的XML對象的某些屬性。 我可以很好地訪問數據,然后更改它,並通過LINQpad Dump()進行更新。 但是,當我運行SubmitChanges(),然后從LINQ(或SQL Server)運行另一個查詢時,相關行尚未更新。

我嘗試使用谷歌搜索這個問題,我發現一個常見的問題與主鍵有關,我已經在SQLStudio中驗證了使用中的表確實具有主鍵,所以我認為這不是問題。

另外,您還可以在第一行中看到將DataContext的Log設置為Console.Out。 啟用此功能后,我得到了幾行代碼來驗證我的第一個查詢(select)是否有效。 但是,在運行我的UpdateRecord函數或運行SubmitChanges之后,我在控制台中沒有得到任何種類的UPDATE查詢,也許更新邏輯不正確?

更新 :我在運行SubmitChanges之前添加了GetChangeSet()。Dump(),ChangeSet中有0個更新和0個插入,確認我的UpdateRecord函數未正確更新任何內容。

void Main() {
    Log = Console.Out;

    AnonymiseCommand("MyCommandName");

}

void AnonymiseCommand(string command) { 

    // Note we need to pluralise the table name for the LINQ query
    // Table is actually called ChangeSet_Detail
    var results = ChangeSet_Details
                    .Where(c => c.Command.ToString().Contains(command) )
                    .ToDictionary(row => (int) row.ChangeSetID,
                                  row => row.Changes);

    int commandCount = results.Count;
    Print("Command count for " + command + " is " + commandCount + ".");    

到目前為止,一切正常,commandCount正確返回1,結果字典包含正確的值。 接下來,我將遍歷字典,並嘗試使用與dicts鍵匹配的ID更新行,並將XElement映射到鍵。

    foreach (KeyValuePair<int, XElement> entry in results) {
        AnonymiseXml(entry.Value);
        UpdateRecord(entry.Key, entry.Value);
    }

}

// This function isn't so important, it anonymises the attributes and seems to work
void AnonymiseXml(XElement xml) {
    // anonymise attributes
    var attributes = xml.Attributes();
    foreach(var attr in attributes) {
        attr.Value = new String('?', attr.Value.Length);
    }

    // repeat across child nodes
    var kiddies = xml.Elements();
    foreach (var kid in kiddies) {
        AnonymiseXml(kid);
    }
}

void UpdateRecord(int rowID, XElement newXml) {
    ChangeSet_Detail entry = 
        (from c in ChangeSet_Details
            where c.ChangeSetID == rowID
            select c).Single();

    entry.Dump();
    entry.ChangeSetID = rowID;
    entry.Changes = newXml;
    entry.Dump();

    GetChangeSet().Dump();

    try{
        SubmitChanges();
    } catch(Exception e) {
        e.Dump();
    }

}

我試圖將更改提交到數據庫的地方是UpdateRecord函數。 我傳入當前正在查看的行ID和新的XML元素。 當我檢索條目時,似乎我的更改仍在第一個條目上生效。Dump()我可以看到屬性已匿名化。 無論如何,我都會更改條目XML列(列名為Changes),最后調用SubmitChanges()。

然后,如果我在LINQpad或SQL Server中查詢該表,則我的更改沒有生效,並且屬性沒有被匿名化。

我自己解決了。

所以我認為問題是,我正在就地更改XML值,即對象引用從不更改,XElement及其對象保持不變,但是我更改了屬性的字符串值,也許這意味着LINQ未檢測到任何更改。

當我告訴LINQ在運行SubmitChanges()之前刷新更改時,將記錄更新並保留我的更改。

添加的行很簡單:

Refresh(RefreshMode.KeepCurrentValues, entry);
      ComplaintComment tcc = new ComplaintComment();

            var Dat = DateTime.Now;

            tcc.comp_Id =1;
            tcc.cust_Id = 1;

            tcc.cc_Comments = txtComment.Text;

dbContext.ComplaintComments.InsertOnSubmit(tcc);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM