ADO.. NET实体模型更新
本文关键字:更新 实体模型 NET ADO | 更新日期: 2023-09-27 18:08:55
嗨,我正试图从数据库更新记录,但由于某种原因,记录没有更新。我正在使用ADO。. NET实体框架4.0
public void updateCredits(string username)
{
myDBEntities Entity = new myDBEntities();
User u = Entity.Users.SingleOrDefault(u => u.username == username)
u.firstname = "name";
u.credits = 11.5;
Entity.SaveChanges();
}
我试着重新启动VS,甚至SQL Server,但没有运气。我做错什么了吗?
看起来变更跟踪有问题。您可以尝试手动将User
实体标记为已更改,如下所示:
public void updateCredits(string username)
{
myDBEntities Entity = new myDBEntities();
User u = Entity.Users.SingleOrDefault(u => u.username == username)
u.firstname = "name";
u.credits = 11.5;
Entity.Entry(u).State = EntityState.Modified; //<-- manually indicate the entity was changed
Entity.SaveChanges();
}
如果上面的代码有效,那么很有可能ChangeTracking被禁用了