[英]Updating record not working in LINQ to SQL with primary key set in class
I have code which should update ma database:我有应该更新 ma 数据库的代码:
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.我检查了 web 中所有可能的解决方案,但对我没有任何帮助。
In data class primary key is marked for the table:在数据 class 中为表标记了主键:
[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.我还尝试在不更改数据类的情况下从表中删除 PK,但这也不起作用。
Problem resolved.问题已解决。 I don't know why but update.roz = update.roz++;
我不知道为什么,但是update.roz = update.roz++;
Was not incrementing the value and simply chaniging it to update.roz = update.roz + 1;
没有增加值,只是将其更改为update.roz = update.roz + 1;
worked for me.为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.