Using EF and Dapper in the same project

4

I'm making a simple insert of a user class, it's taking almost 10 seconds to save the information. So I tried to find other solutions to solve my performance problem and found Dapper .

My question is there any way to use the two ORMs in my project? because I liked EF in the question of mapping and migrations because I use it as Code First.

To get at this conclusion of slowness I used this article as a theoretical framework.

The classes I'm using:

User :

   [Table("Usuario", Schema = "public")]
    public class Usuario : PessoaBase
    {
        private string login;
        private string senha;

        public Usuario(string nome, string email, int status, string login, string senha, DateTime dtCriacao) : base( nome, status, dtCriacao, email)
        {
            this.login = login;
            this.senha = senha;

        }

        [DisplayName("Login")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Login deve ser preenchido!")]
        [StringLength(50)]
        [Index("Ix_UsuarioLogin", IsUnique = true)]
        public string Login
        {
            get { return login; }
            set { login = value; }
        }

        [DisplayName("Senha")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Senha deve ser preechida!")]
        [StringLength(20)]
        public string Senha
        {
            get { return senha; }
            set { senha = value; }
        }

    }

Repository :

public abstract class Repositorio<TEntity> : IDisposable,
           IRepositorio<TEntity> where TEntity : class
    {
        BaseContexto ctx = new BaseContexto();
        public IQueryable<TEntity> GetAll()
        {
            return ctx.Set<TEntity>();
        }

        public IQueryable<TEntity> Get(Func<TEntity, bool> predicate)
        {
            return GetAll().Where(predicate).AsQueryable();
        }

        public TEntity Find(params object[] key)
        {
            return ctx.Set<TEntity>().Find(key);
        }

        public void Atualizar(TEntity obj)
        {
            ctx.Entry(obj).State = EntityState.Modified;
        }

        public void SalvarTodos()
        {
            ctx.SaveChanges();
        }

        public void Adicionar(TEntity obj)
        {
            ctx.Set<TEntity>().Add(obj);
        }

        public void Excluir(Func<TEntity, bool> predicate)
        {
            ctx.Set<TEntity>()
                .Where(predicate).ToList()
                .ForEach(del => ctx.Set<TEntity>().Remove(del));
        }

        public void Dispose()
        {
            ctx.Dispose();
        }
    }

To save a user I just do this:

uDal.Adicionar(u);
 uDal.SalvarTodos();

I'm using NetFramework 4.5, visual studio 2013 and postgresql BD.

    
asked by anonymous 05.05.2017 / 02:57

1 answer

5
  

My question is there any way to use the two ORMs in my project? because I liked EF in the question of mapping and migrations because I use Code First.

Yes, look:

var query = db.Database.Connection.Query(...); // <- Isto é Dapper em cima da connection do Entity Framework.

Remembering that Dapper is a framework -extension implemented over IDbConnection .

PS: Leave repository. I already talked about it .

    
05.05.2017 / 03:01