So basically I have this field in XML that I want to update. It is parsed out through XML and I am not sure how to update this single record from this single instance.
var dataContext =
new RequestFormsDBDataContext(ConfigManager.Data.ConnectionString);
var userForm = (
from i in dataContext.RequestFormItems
join c in dataContext.RequestFormInstances on i.TypeGuid equals c.TypeGuid
where i.Id == FormID
select new {
i.Id,
XmlData = XElement.Parse(i.XML)
}
).ToList();
// Parsing out XML Data
var userFormParced = (
from i in userForm
select new FormItem {
FormId = i.Id,
DateTimeCompleted = i.XmlData.Element("DateTimeCompleted").Value
}
).FirstOrDefault();
RFDateTimeCompleted = userFormParced.DateTimeCompleted;
// Code that isnt working
userFormParced.DateTimeCompleted = "New Data";
dataContext.SubmitChanges();
This won’t work because you aren’t changing the instance that you retrieve from the database. You are creating a new object using the values from the original – twice – and then changing it. The LINQ-to-SQL context has no knowledge that you’re changing database rows, just some unrelated XML that you’ve constructed.
Firstly, the value you retrieve from the database is being put into an XElement
and then you retrieve the ‘value’ from a node and put it into another new object
It’s lost any reference to the LINQ-to-SQL context by this stage. Instead you need to change it in place.
If you’re using XML in your database, you should be using XML columns which map to XML properties in your LINQ-to-SQL objects. You’re not, so we’ll implement a workaround.
Try something like this.