Reputation: 173
I have this method :
public void Update(DBS.BankAccount entity)
{
try
{
using (var _nahidContext = new NahidContext())
{
_nahidContext.BankAccounts.Attach(entity);
var bankAccountElement = _nahidContext.Entry(entity);
bankAccountElement.CurrentValues.SetValues(entity);
_nahidContext.SaveChanges();
//__________ or ___________
//var bankAccountElement = FindById(entity.Id);
//if (_nahidContext.Entry(bankAccountElement).State == System.Data.Entity.EntityState.Detached)
//{
// _nahidContext.BankAccounts.Attach(bankAccountElement);
//}
////_nahidContext.Entry(bankAccountElement).State = System.Data.Entity.EntityState.Modified;
//_nahidContext.SaveChanges();
}
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
}
Which run without any error but my bankAccountElement
does not change.
Please help me.
Upvotes: 0
Views: 39
Reputation: 2354
First you attach (entity state= unchanged), then you set values with same values than attached entity, so it keeps unchanged.
You should do this
_nahidContext.BankAccounts.Attach(entity);
var bankAccountElement = _nahidContext.Entry(entity);
bankAccountElement.State = EntityState.Modified;
You can read more here
Upvotes: 2