我用EntityFramework来操作保存数据,只能对其添加显示功能,我应该怎么来修改数据,能不能像操作数据库一样,根据主键来修改数据,类里面的属性能不能也这样修改。请教个位大虾帮忙啊。谢谢啦!
建议更详细描述一下问题。
对于EF修改数据,我写过几篇博客:
private void UpdateObject()
{ ModelContainer edm = new ModelContainer(SetDBConnection());
Users users = edm.Users.FirstOrDefault(u => u.UserName == "张三");
users.Address = "北京";
int result = edm.SaveChanges();
}
ef修改数据必须先要查询出来,再修改,如果批量修改的话,建议不要采用ef
Models.UserDetails modelsUserDetails = new Models.UserDetails();
modelsUserDetails.Email = user.Email;
modelsUserDetails.Tel = user.Tel;
modelsUserDetails.UserID = user.UserID;
modelsUserDetails.UserName = user.UserName;
modelsUserDetails.Memo = user.Memo;
UHDEntities.Entry(modelsUserDetails).State = EntityState.Modified;
UHDEntities.Commit();