All was running along fine till I tried a simple SQL Server update from Linq. My code is:
DataClasses1DataContext db = new DataClasses1DataContext("mySQLServer");
db.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["mySQLServer"].ToString();
var results = (from bl in db.a_Bellows_Lots
where bl.Job == this.theJob
where bl.BellowsLot == this.theLot
select bl).SingleOrDefault();
results.Quantity = this.theQuantity;
try
{
db.SubmitChanges();
}
catch (Exception ex)
{
error += " " + ex.Message;
But the value is not getting changed. I modeled this in LinqPad with this;
string JobNumber = "A2559038A";
string LotNumber = "17213A";
var results = (from bl in a_Bellows_Lots
where bl.Job == JobNumber
where bl.BellowsLot == LotNumber
select new
{
bl.Quantity
}).SingleOrDefault();
results.Quantity = 1;
this.SubmitChanges();
This gives me a design time error of "Propery or indexer 'AnonymousType#1.Quantity' cannot be assigned to -- it is read only."
I've used this design model before and it works:
DataClasses1DataContext db = new DataClasses1DataContext("mySQLServer");
db.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["mySQLServer"].ToString();
var results = (from x in db.a_Cleaning_Logs
where x.CleaningLogID == CleaningLogID
select x).SingleOrDefault();
results.EndTime = EndDate;
results.EmployeeOut = employeeOut;
results.CleaningDone = cleaningDone;
db.SubmitChanges();
What is the difference; what am I doing wrong?
Anonymous objects are read-only.
So the following should work
but not if you use
new { bl.Quantity}
instead of returning the whole record.If the former does not work, then perhaps your table does not have an identity column.
If you are using LinqPad, you can call
this.GetChangeSet().Dump();
before you submit the changes to see if there are any changes.