/// <summary> /// Updates the entity, based on its unique identifier. /// </summary> /// <param name="entity">Entity to be updated.</param> /// <exception cref="AirplaneException">Throws when something goes worng. The InnerException can be used to have more details.</exception> /// <returns></returns> public async Task <Airplane> UpdateAsync(Airplane entity) { try { var airplane = default(Airplane); using (_context = new GolContext(_configuration)) { var entry = _context.Airplanes.Update(entity); var rowsAffected = await _context.SaveChangesAsync(); if (rowsAffected > 0) { airplane = entry.Entity; } } if (airplane == null) { throw new AirplaneException("O avião não foi atualizado na base de dados."); } return(airplane); } catch (Exception e) { throw new AirplaneException("Não foi possível atualizar o avião informado.", e); } }
public AirplanesDomain(IConfiguration configuration) { _configuration = configuration; using (_context = new GolContext(_configuration)) { _context.Database.Migrate(); } }
public virtual bool Gravar(ref T objeto) { try { var model = objeto as Airplanes; var id = (long)objeto.GetType().GetProperty("Id").GetValue(objeto); Contexto = new GolContext(); if (id == 0) { Contexto.Set <T>().Add(objeto); } else { var atual = Contexto.Set <T>().Find(id); //var atual = (from c in Contexto.Airplanes // where c.Id == id // select c).First(); foreach (var property in atual.GetType().GetProperties()) { try { property.SetValue(atual, objeto.GetType().GetProperty(property.Name).GetValue(objeto)); } catch (Exception e) { var erro = e.Message; } } //atual.Modelo = model.Modelo; //atual.Passageiros = model.Passageiros; } Contexto.SaveChanges(); return(true); } catch (Exception ex) { throw ex; } finally { Contexto.Dispose(); } }
/// <summary> /// Removes the specified entity from database. /// </summary> /// <param name="entity">Entity to be deleted.</param> /// <exception cref="AirplaneException">Throws when something goes worng. The InnerException can be used to have more details.</exception> /// <returns></returns> public async Task DeleteAsync(Airplane entity) { try { using (_context = new GolContext(_configuration)) { _context.Airplanes.Remove(entity); var rowsAffected = await _context.SaveChangesAsync(); } } catch (Exception e) { throw new AirplaneException("Não foi possível remover o avião informado.", e); } }
/// <summary> /// Gets a specific entity based on its unique identifier. /// </summary> /// <exception cref="AirplaneException">Throws when something goes worng. The InnerException can be used to have more details.</exception> /// <returns></returns> public async Task <Airplane> GetByIdAsync(int id) { try { var airplane = default(Airplane); using (_context = new GolContext(_configuration)) { airplane = await _context.Airplanes.FirstOrDefaultAsync(a => a.ID.Equals(id)); } return(airplane); } catch (Exception e) { throw new AirplaneException("Não foi possível buscar o avião solicitado.", e); } }
/// <summary> /// Gets all entities available to database. /// </summary> /// <exception cref="AirplaneException">Throws when something goes worng. The InnerException can be used to have more details.</exception> /// <returns></returns> public async Task <IEnumerable <Airplane> > GetAsync() { try { var airplanes = default(IEnumerable <Airplane>); using (_context = new GolContext(_configuration)) { airplanes = await _context.Airplanes.ToListAsync(); } return(airplanes); } catch (Exception e) { throw new AirplaneException("Não foi possível buscar os aviões disponíveis.", e); } }
public virtual ICollection <T> Listar() { try { Contexto = new GolContext(); return(Contexto.Set <T>().ToList()); } catch (Exception ex) { throw ex; } finally { Contexto.Dispose(); } }
public virtual bool Excluir(long id) { try { Contexto = new GolContext(); var atual = Contexto.Set <T>().Find(id); Contexto.Set <T>().Remove(atual); Contexto.SaveChanges(); return(true); } catch (Exception ex) { throw ex; } finally { Contexto.Dispose(); } }
public AirplaneRepository(GolContext context) : base(context) { }
public UnitOfWork(GolContext context) { _context = context; }
public AirplaneRepository(GolContext context) { _context = context; }
public AirplaneRepository(GolContext dbContext) : base(dbContext) { _context = dbContext; }
protected BaseRepository(GolContext db) { Db = db; DbSet = db.Set <TEntity>(); }