How to Save Data Console Application using Entityframework

Asked

Viewed 114 times

1

I made a console application using the Entity Framework, I’m just having a hard time implementing the methods of my repository class that I inherit from an interface,:

public interface IBaseRepositorio<TEntity> where TEntity : class
{
    //Métodos para obter todos contatos e obter por Id
    List<Contato> ObterTodosContatos();
    Contato ObterContatoId(int id);

    //Métodos de inserção,atualização,exclusão
    bool IncluirContato(Contato contato);
    bool AtualizarContato(Contato contato, String colunaAtual);
    bool DeletarContato(int id);
}

My Interfacebase.

Here my Interface inherited from the Base Interface:

 public interface IContatoRepositorio:IBaseRepositorio<Contato>
 {

 }

But here in my concrete repository class I can’t perform the operations to run my CRUD,:

public class ContatoRepositorio : IContatoRepositorio<Contato>
{
    protected ProjectTestsContext Db = new ProjectTestsContext();


    public Contato ObterContatoId(int id)
    {
        return this.Db.Contatos.SqlQuery("Select * From Contato where Contatoid = @Contatoid", new { Id = id }).FirstOrDefault();
    }

    public List<Contato> ObterTodosContatos()
    {
        return this.Db.Contatos.SqlQuery("Select * From Contatos").ToList();
    }

    public bool IncluirContato(Contato contato)
    {
        try
        {
            string sql = "INSERT INTO Contato(Nome,Sobrenome,Empresa,Titulo) values(@Nome,@Sobrenome,@Empresa,@Titulo);SELECT CAST(SCOPE_IDENTITY() as int)";
            var returnId = Db.Contatos.SqlQuery(sql, contato).SingleOrDefault();
            contato.Contatoid = returnId.ToString();
        }
        catch (Exception)
        {

            return false;
        }
        return true;
    }

    public bool AtualizarContato(Contato contato, string colunaAtual)
    {
        string query = "Update Contato set " + colunaAtual + " =@" + colunaAtual + "Where Contatoid=@Contatoid";
        var count = Db.Execute(query, contato);
        return true;

    }

    public bool DeletarContato(int id)
    {
        // var affectdrowns = Db.Execute("Delete From Contato where Contatoid = @Contatoid", new { Contatoid = id });
        //   return affectdrowns > 0;
        return true;
    }



}

In the contact update method, the Db.Execute section, do not think the action he would have to do,I found only Entry, but I can not update.

If anyone can help me how best to accomplish these methods,.

Thank you

  • you are creating your Dbset ? on your Projecttestscontext

1 answer

0

If you’ve created your own Dbset, you can create a simpler CRUD with the Entity Framework.

See how it would look.

public class ContatoRepositorio : IContatoRepositorio<Contato>
{
    protected ProjectTestsContext Db = new ProjectTestsContext();


    public Contato ObterContatoId(int id)
    {
        return this.Db.Contatos.FirstOrDefault(x => x.Id = id );
    }

    public List<Contato> ObterTodosContatos()
    {
        return this.Db.Contatos.ToList();
    }

    public bool IncluirContato(Contato contato)
    {
        try
        {
            Db.Contato.Add(contato);
            Db.SaveChanges();   
        }
        catch (Exception)
        {

            return false;
        }
        return true;
    }

    public bool AtualizarContato(Contato contato, string colunaAtual)
    {
        Db.Entry(contato).State = EntityState.Modified;
        Db.SaveChanges();
        return true;
    }

    public bool DeletarContato(int id)
    {
        var obj = ObterContatoId(id);
        Db.Categorias.Remove(obj);
        Db.SaveChanges();
        return true;
    }
}

See a functional example here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.