Using EF and Dapper in the same project

Asked

Viewed 359 times

4

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

My doubt there is some way to use both ORMs in my project? because I liked the EF in the question of mapping and Migrations because I use as Code First.

To reach this conclusion of slowness I used this article as a theoretical reference.

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 just do it this way:

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

OBS: I’m using Netframework 4.5, visual studio 2013 and BD postgresql.

  • The main thing would be to identify the slowness, the Entity Framework is not to be so slow 10 seconds is a lot, must have problems as it is in the programming, on the repository layer already have problems as you did, because for each item will have a different context, this is a problem many do, has to be done a little differently, and some developers are against, but, I see in your question that the problem is bigger, think 10 seconds is too long. Take a look at your project in the background, just a hint.

  • One more thing I use EF in my projects and have no problem at the time of recording or even recovery that is more expensive, works very well inclusive.

1 answer

5


Is there any way I can use both Orms in my project? because I liked the EF in the issue of mapping and Migrations because I use as Code First.

Yeah, check it out:

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 told you about that.

  • Yes I read several topics already your Gypsy talking about the repositories, but did not understand the reason. One of them would be slow problem too?

  • 1

    See my response without dynamic reading. It took me years to write it, with experiences I had in projects and companies I went through. Simply not worth using. Causes slowness if you observe the Fallacy 1.

  • Thank you, but does Voce use EF in your projects? Another question, I can (sure, feasible) use EF only for mapping and version control with migrations, but then use Dapper for data manipulation and leave EF aside ?

  • 1

    " Voce uses EF in its projects?" yes. " can (sure, feasible) use EF only for mapping and version control with migrations, but why use Dapper for data manipulation and leave EF aside?" , will give you more work, but yes, you can. I have a course on ASP.NET MVC with a chapter on Dapper in which I explain all this.

  • Gypsy where can I get the course? If you have several courses and can pass the link, I get them. because I’m marrying into finding courses that teach wrong :X

  • Myself. Look me up on Facebook. This is the menu, but the lessons are individual, and you mark day and time of class.

Show 1 more comment

Browser other questions tagged

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