[英]LINQ SQL query, SubmitChanges is not submitting changes to DB
I am writing a LINQpad script to anonymise certain attributes of an XML object, stored in XML columns of a table. 我正在编写LINQpad脚本来匿名化存储在表的XML列中的XML对象的某些属性。 I can access data fine, and then change it, and through LINQpad Dump()s, it seems to be updated.
我可以很好地访问数据,然后更改它,并通过LINQpad Dump()进行更新。 However when I run SubmitChanges() and then run another query from LINQ (or SQL Server), the relevant row has not been updated.
但是,当我运行SubmitChanges(),然后从LINQ(或SQL Server)运行另一个查询时,相关行尚未更新。
I've tried googling this problem, a common issue I found is related to the primary key, I've verified in SQLStudio the table in use does have a primary key, so I don't think this is the issue. 我尝试使用谷歌搜索这个问题,我发现一个常见的问题与主键有关,我已经在SQLStudio中验证了使用中的表确实具有主键,所以我认为这不是问题。
Also, you can see in the first line I've set Log on the DataContext to Console.Out. 另外,您还可以在第一行中看到将DataContext的Log设置为Console.Out。 With this on, I get a couple of lines verifying my first query (the select) works.
启用此功能后,我得到了几行代码来验证我的第一个查询(select)是否有效。 However, I don't get any sort of UPDATE query in the console after running my UpdateRecord function, or running SubmitChanges, maybe the update logic is incorrect?
但是,在运行我的UpdateRecord函数或运行SubmitChanges之后,我在控制台中没有得到任何种类的UPDATE查询,也许更新逻辑不正确?
Update : I added GetChangeSet().Dump() before running SubmitChanges, there are 0 Updates and 0 Inserts in the ChangeSet, confirming my UpdateRecord function is not correctly updating anything. 更新 :我在运行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 + ".");
Everything up to this point works fine, commandCount correctly returns 1, and the results dictionary contains the correct values. 到目前为止,一切正常,commandCount正确返回1,结果字典包含正确的值。 Next I will iterate through the dictionary, and attempt to update a row with an ID matching the dicts key, with the XElement mapped to the key.
接下来,我将遍历字典,并尝试使用与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();
}
}
The UpdateRecord function is where I am trying to commit the changes to the DB. 我试图将更改提交到数据库的地方是UpdateRecord函数。 I pass in the row ID that we are currently looking at, and the new XML element.
我传入当前正在查看的行ID和新的XML元素。 When I retrieve the entry, it seems my changes are still in effect, on the first entry.Dump() I can see the attributes are anonymised.
当我检索条目时,似乎我的更改仍在第一个条目上生效。Dump()我可以看到属性已匿名化。 I change the entries XML column (column is named Changes) anyway, and finally call SubmitChanges().
无论如何,我都会更改条目XML列(列名为Changes),最后调用SubmitChanges()。
If I then query the table in LINQpad or SQL Server, my changes have not taken effect and the attributes have not been anonymised. 然后,如果我在LINQpad或SQL Server中查询该表,则我的更改没有生效,并且属性没有被匿名化。
Solved it myself. 我自己解决了。
So I think the issue is, I am changing the XML values in place, ie the object references never change, the XElement and its objects stay the same, but I change the string values of the attributes, perhaps this means LINQ detects no changes. 所以我认为问题是,我正在就地更改XML值,即对象引用从不更改,XElement及其对象保持不变,但是我更改了属性的字符串值,也许这意味着LINQ未检测到任何更改。
When I tell LINQ to refresh changes before running SubmitChanges(), the Update is logged and my changes are persisted. 当我告诉LINQ在运行SubmitChanges()之前刷新更改时,将记录更新并保留我的更改。
The line added is simply: 添加的行很简单:
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.