public string Update(T model)
 {
     using (Cs20483Context context = new Cs20483Context())
     {
         context.Entry <T>(model).State = EntityState.Modified;
         context.SaveChanges();
     }
     return("Alterado com sucesso");
 }
 public string Create(T model)
 {
     using (Cs20483Context context = new Cs20483Context())
     {
         context.Set <T>().Add(model);
         context.SaveChanges();
     }
     return("Inserido com sucesso");
 }
        public List <T> ListAll()
        {
            List <T> list;

            using (Cs20483Context context = new Cs20483Context())
            {
                list = context.Set <T>().ToList();
            }
            return(list);
        }
 public string Delete(int id)
 {
     using (Cs20483Context context = new Cs20483Context())
     {
         T model = GetById(id);
         context.Entry <T>(model).State = EntityState.Deleted;
         context.Set <T>().Remove(model);
         context.SaveChanges();
     }
     return("Deletado com sucesso");
 }
        public T GetById(int id)
        {
            T model;

            using (Cs20483Context context = new Cs20483Context())
            {
                model = context.Set <T>().Find(id);
                // --- Filtro por id usando
                //model = context.Produto.Where(p=>p.Id==id).FirstOrDefault();
            }
            return(model);
        }
Beispiel #6
0
        public T GetById(int id)
        {
            T model;

            using (Cs20483Context context = new Cs20483Context())
            {
                model = context.Set <T>().Find(id);
                //ou
                //model = context.Produto.where(p=>p.Id==id).FirstOrDefault(); //você deve oscolher o que ele deve trazer (primeiro, ultimo, unico...)
            }

            return(model);
        }
Beispiel #7
0
        //criar os métodos do CRUD
        public List <T> ListAll()
        //public List<Produto> ListAll(string digitado)
        {
            List <T> list = new List <T>();

            using (Cs20483Context context = new Cs20483Context()) //quando usamos o using ele já vai limpando as conexões
            {
                list = context.Set <T>().ToList();                //vai pegar a tabela, selecionar todos, converter para o objeto e vai listar todos
                                                                  //lambda = context.Produto.Where(a=>a.Id ==10).ToList();
                                                                  //list = context.Produto.Where(a=>a.Nome.Contains(Digitado)).ToList()

                //var query = context.Produto.Where(p => p.Id == 10);
                //    query.Where().... ---seria possível fazer varias regras, podemos também fazer varios ifs e dependendo do resultado executar a função no final dos ifs
            }

            return(list);
        }