Reputation: 11356
im trying to update all records in a sql table using the following code but no data is updated. does anyone know why?
using (DataContext db = new DataContext())
{
foreach (Item record in db.Items)
{
record.Description += "bla";
db.SubmitChanges();
}
}
code for setter:
[Column(Storage="_Description", DbType="NVarChar(400) NOT NULL", CanBeNull=false)]
public string Description
{
get { return this._Description; }
set {
if ((this._Description != value))
{
this._Description = value;
}
}
}
Upvotes: 3
Views: 7743
Reputation:
I realize this is a really old question, but it remains unanswered.
The answer provided by Simon Fox is technically correct, and the suggestion by BFree is a big time saver.
Yet, if your table does not have a Primary Key specified in the table, the DBML object will not detect a change and does not know how to update your records.
Upvotes: 0
Reputation: 10561
From the details of the setter you have posted in the comments, your Description property has not been correctly created for notification of property changes. Did you write the property yourself or was it generated by the VS2008 tooling?
Your Item class (all Linq to Sql entities for that matter should implement INotifyPropertyChanging and INotifyPropertyChanged) which will give you both PropertyChanging event and PropertyChanged events, if you used the VS2008 tools you should get a couple of methods like the following in your entity classes:
protected virtual void SendPropertyChanging(string propertyName)
{
if (this.PropertyChanging != null)
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
protected virtual void SendPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Now within your setter of your property you should use these methods to raise the required events:
[Column(Name="Description", Storage="_Description",
DbType="NVarChar(400) NOT NULL", CanBeNull=false)]
public string Description
{
get { return this._Description; }
set
{
if ((this._Description != value))
{
this.SendPropertyChanging("Description");
this._Description = value;
this.SendPropertyChanged("Description");
}
}
}
I also noticed you don't have the Name property set in your column attribute so add it just in case (I have it included in my example assuming your column name is "Description").
Upvotes: 3
Reputation: 9084
Maybe you need to supply a connection string.
using (DataContext db = new DataContext(_MyConnectionString))
{
foreach (Item record in db.Items)
{
record.Description += "bla";
}
db.SubmitChanges();
}
I've had some strange issues with the DataContext when not supplying a connection string.
Upvotes: 2
Reputation: 8037
I will point out you can SubmitChanges after the loop closes... This won't solve your problem but it will help out a bit.
Upvotes: 2
Reputation: 103742
Out of curiosity, see if moving the SubmitChanges() outside of the loop makes a difference:
using (DataContext db = new DataContext())
{
foreach (Item record in db.Items)
{
record.Description += "bla";
}
db.SubmitChanges();
}
Upvotes: 4