I have a generic repository and I have a Controllers for each of my entities.
eventoContactIdOcorrenciaRecebida = dtoEventoContactId.ObterPorCodigoEvento(Convert.ToInt32(codigoEventoContactID));
When I do the search it returns the value correctly, however if for some reason I change the value in the bank, and do the search again it does not update. It's like she's looking in the cache, not the bank. If I close the application and open it again it does the search correctly. From what I researched here on vi I understood that I have to use AsNotracking()
. But how can I use it in this command line above.
I'll edit here by putting more information
The eventContacIdOurCredit is an instance of Model ContactIDEvent
The dtoEventoContactId is the Controllers
Controllers:
public class ContactIDEventoControllers : Repository<ContactIDEvento>, IContactIDEvento
{
public ContactIDEvento ObterPorCodigoEvento(int intCodigo)
{
return Buscar(c => c.Codigo == intCodigo).FirstOrDefault();
}
}
Template:
public class ContactIDEvento
{
public int ContactIDEventoID { get; set; }
public int Codigo { get; set; }
public string Nome { get; set; }
}
Interface Repository
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
TEntity Adicionar(TEntity obj);
TEntity ObterPorId(int id);
IEnumerable<TEntity> ObterTodos();
TEntity Atualizar(TEntity obj);
void Remover(int id);
IEnumerable<TEntity> Buscar(Expression<Func<TEntity, bool>> predicate);
int SaveChanges();
}
Repository
public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity: class
{
protected MonitoramentoContext Db;
protected DbSet<TEntity> DbSet;
public Repository()
{
Db = new MonitoramentoContext();
DbSet = Db.Set<TEntity>();
}
public TEntity Adicionar(TEntity obj)
{
var objAdd = DbSet.Add(obj);
SaveChanges();
return objAdd;
}
public TEntity Atualizar(TEntity obj)
{
var entry = Db.Entry(obj);
DbSet.Attach(obj);
entry.State = EntityState.Modified;
SaveChanges();
return obj;
}
public IEnumerable<TEntity> Buscar(Expression<Func<TEntity, bool>> predicate)
{
return DbSet.Where(predicate);
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~Repository() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
public TEntity ObterPorId(int id)
{
return DbSet.Find(id);
}
public IEnumerable<TEntity> ObterTodos()
{
return DbSet.ToList();
}
public void Remover(int id)
{
DbSet.Attach(DbSet.Find(id));
DbSet.Remove(DbSet.Find(id));
SaveChanges();
}
public int SaveChanges()
{
return Db.SaveChanges();
}
}