I have a generic method to insert or update method that works but as I am a new to this. Are there any potential issues with my current implementation that are not handled here?
public class Repository<T> : IRepository<T> where T : BaseModel
{
protected DbContext Context;
protected readonly IDbSet<T> dbSet;
public void InsertOrUpdate(T entity)
{
DbSet<T> dbSet = Context.Set<T>();
var e = this.Find(entity.Id);
if (e != null)
{
DbEntityEntry<T> entry;
entry = Context.Entry(e);
entry.CurrentValues.SetValues(entity);
dbSet.Attach(entry.Entity);
entry.State = EntityState.Modified;
}
else
{
dbSet.Add(entity);
}
}
public T Find(object id)
{
return dbSet.Find(id);
}
}
Comments
Post a Comment