Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use dapper-dot-net with Entity Framework?

I am trying to use dapper-dot-net to speed up some area of my asp.net mvc application. I am using EF5 Code first also.

Since dapper-dot-net is just some extensions for IDbConnection, can i just use

DbContext.Database.Connection 

to use dapper-dot-net? I test it is working. However, i am not sure this is the right way to use it? Especially, when I use that way, will Entity Framework still has some impact that could hurt the performance?

like image 823
liuhongbo Avatar asked Jul 29 '13 20:07

liuhongbo


People also ask

Can you use Dapper with Entity Framework?

Dapper is super awesome to handle complex queries that sport multiple joins and some real long business logic. Entity Framework Core is great for class generation, object tracking, mapping to multiple nested classes, and quite a lot more. So it's usually Performance and Features when talking about these 2 ORMs.

Can I use Entity Framework with .NET Core?

NET Framework, as Entity Framework 6 doesn't support . NET Core. If you need cross-platform features you will need to upgrade to Entity Framework Core. The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets .

Can you use Entity Framework with net framework?

You can use EF Core in APIs and applications that require the full . NET Framework, as well as those that target only the cross-platform .

Can I use Dapper in .NET Core?

Using Dapper Queries in ASP.NET Core Web API Then inside the using statement, we use our DapperContext object to create the SQLConnection object (or to be more precise an IDbConnection object) by calling the CreateConnection method.

How to implement dapper in Entity Framework?

When you want to have the dapper implementation, call the dapper specific method in that class. As you can see we have removed the existing entity framework code with Dapper code for better performance. I have other methods in the repository that make use of entity framework for reading and writing to the database.

Can I share the EF connection with Dapper?

You can share the EF connection with Dapper. However (although unlikely to be a problem) you should be mindful of concurrency issues (e.g. due to attempts to associate multiple data readers with the same connection).

Should I use Dapper for SQL?

People will prefer Dapper when they want to write the SQL query themselves with optimal performance. Is Dapper SQL Injections safe? Yes, it's 100% safe if you use parametrized queries as you should always do! Does Dapper support Bulk Insert? No, but a popular third-party library does: Dapper Plus. It's a good example of Dapper's extensibility.

What is the difference between dapper and EF Core?

Due to this, some developers prefer to use Dapper for read operations and EF Core for write operations involving complex objects to be written into the database as transforming them into Dapper queries itself may be a cumbersome task.


3 Answers

Using Dapper could be a significant performance improvement in certain scenarios.

You can share the EF connection with Dapper. However (although unlikely to be a problem) you should be mindful of concurrency issues (e.g. due to attempts to associate multiple data readers with the same connection).

If you do run into such issues, you have the option of giving a new connection to Dapper using the connection string (DbContext.Database.Connection.ConnectionString), instead of sharing the connection.

like image 71
Eren Ersönmez Avatar answered Sep 30 '22 02:09

Eren Ersönmez


Yes, you can use it that way. Since Dapper is just working on extension methods, you can use it for the performance-sensitive areas of your code. And you can continue to use EF for other areas of your code. The queries that you have that are still using EF will not be as fast - but at least the queries using Dapper will be faster.

like image 32
Steve Michelotti Avatar answered Sep 30 '22 04:09

Steve Michelotti


I think you have to rethink about the question. Increasing performance via changing the ORM is not a good technique. It is correct that dapper is faster and lighter than EF but this does not mean that to increase the speed of your application its better to use the dapper. EF is powerful enough to handle the whole data layer if you suffer from performance you have to introduce new feature like caching or no sql db.

you have to see that changing from EF to dapper how much will save time for you ? and how much speed can caching bring to your application.

and adding dapper has other expenses: how would you manage the transaction while you have to point of save and update how you are going to solve the roll back situation, what about the Unit of Work and Repository pattern.

These are the factor that you have to examine before deciding to go for Dapper.

like image 44
Meysam Avatar answered Sep 30 '22 04:09

Meysam