Reputation: 1308
How can I update an entity that is detached from context by AsNoTracking()
?
var _agency = agencyRepository.Get(filter: a => a.Id == agency.Id)
.AsQueryable()
.AsNoTracking()
.FirstOrDefault();
agencyRepository.Update(_agency);
and my Update method already set modified:
public virtual void Update(T entity)
{
dbset.Attach(entity);
dataContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
}
Can I find the previous entity that is attached by datacontext? or any suggestion to prevent Tracking on my User entity?
Upvotes: 8
Views: 16213
Reputation: 3439
When you get your object as "AsNoTracking", it means that it's detached from the context and changes won't be tracked. All you need to do is attach it to the context again.
Here is a sample code:
async Task update_entity() {
ctx.Attach(profile);
profile.image_id = "12345667";
await ctx.SaveChangesAsync();
}
Upvotes: 2
Reputation: 8765
you can change the state of the entity:
ctx.Entry(_agency).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
More Read this or this article.
Upvotes: 10