C # linq update all records in database

im trying to update all records in sql table using the following code, but the data is not updating. Does anyone know why?

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";
                db.SubmitChanges();
            }
        }

code for the 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; 
       }
      }
}
+3
source share
4 answers

Out of curiosity, see if SubmitChanges () moves outside the loop:

        using (DataContext db = new DataContext())
        {
            foreach (Item record in db.Items)
            {
                record.Description += "bla";   
            }
            db.SubmitChanges();
        }
+4
source

From the information about the installer published in the comments, the Description property was not created correctly to notify of changes in properties. Do you write this property yourself or were you created using the VS2008 toolkit?

Item ( Linq to Sql, , INotifyPropertyChanging INotifyPropertyChanged), PropertyChanging, PropertyChanged, VS2008, , . :

    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));
        }
    }

:

[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");
         }
     }
 }

, Name, , ( , - "" ).

+3

, .

using (DataContext db = new DataContext(_MyConnectionString))
{
    foreach (Item record in db.Items)
    {
        record.Description += "bla";
    }
    db.SubmitChanges();
}

DataContext, .

+2

, SubmitChanges ... , .

+2

Source: https://habr.com/ru/post/1717096/


All Articles