简体   繁体   中英

Updating record not working in LINQ to SQL with primary key set in class

I have code which should update ma database:

public static void UPDATEroz(DataClasses1DataContext dc, string nazwavar)
{
    var update = (from p in dc.dataTable
                  where p.nazwa.Contains(nazwavar)
                  select p).First();

    Debug.WriteLine(update.nazwa);
    update.roz = update.roz++;
    Debug.WriteLine("Updating");

    dc.SubmitChanges();
}

But after running this method nothing changes in database. Inserting new records works fine.

I checked every possible solution in the web and nothing worked for me.

In data class primary key is marked for the table:

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Id", DbType="Int NOT NULL", IsPrimaryKey=true)]

I also tried to remove PK from my table without changing dataclass but that didn't work, either.

Problem resolved. I don't know why but update.roz = update.roz++; Was not incrementing the value and simply chaniging it to update.roz = update.roz + 1; worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM