如何使用entityn框架更新db中的特定用户记录 [英] How to update a specific user record in db by using entityn framework
本文介绍了如何使用entityn框架更新db中的特定用户记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用实体框架更新数据库中的特定记录
我的代码如下
I want to update a specific record in database by using entity framework
my code is following
public int UpdateDataToDb(BusinessObjects objects, PersonalData newPersonal, EducationalData newEducational, ContactData newContact)
{
int status = 5;
try
{
/// Updating Status
UserCredential Credentials = newPersonEntity.UserCredentials
.First(cd => cd.UserName == objects.UserName);
Credentials.Status = objects.Status;
/// Updating Data
switch (objects.FormId)
{
case 1:
PersonalData personal = newPersonEntity.PersonalDatas.First(dt=>dt.UserName==objects.UserName && dt.FormName==objects.FormName);
/// Suggest Code Here
default:
break;
}
/// Saving Changes to Db
return newPersonEntity.SaveChanges();
}
catch (Exception ex)
{
CatchError(ex);
return 3;
}
}
i想要更新它对象告诉我最好的方式
提前感谢
i want to update it by object tell me the best way
thanks in advance
推荐答案
这很容易。这将在您的///建议代码部分中找到。
It's fairly easy. This goes in your "/// Suggest Code Here" section.
...
personal.PropertyToChange = newValue;
newPersonEntity.Entry(personal).State = EntityState.Modified;
...
调用DbContext.SaveChanges()时,将存储新值。
When DbContext.SaveChanges() is called the new value will be stored.
这篇关于如何使用entityn框架更新db中的特定用户记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文