How to save Console Application data using EntityFramework

1

I made a console application using Entity Framework , but I'm having a hard time implementing the methods of my repository class that inherits from an interface, I've made my interface classes and my concrete repository class like this:

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 is my Interface that inherits from the Base Interface:

 public interface IContatoRepositorio:IBaseRepositorio<Contato>
 {

 }

But here in my concrete repository class I can not perform operations to run my CRUD, it follows the class:

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 excerpt does not find the action that it would have to do, I found only the Entry, but I can not do the update.

If someone can help me how to best perform these methods, thank you, even for me to follow the reasoning.

Thank you

    
asked by anonymous 20.02.2018 / 21:08

1 answer

0

If you have created your 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 working example here .

    
21.02.2018 / 14:26