我能否在Entity Framework中使用dapper-dot-net?

24

我试图使用dapper-dot-net来加速我的asp.net mvc应用程序的某些区域。 我也在使用EF5 Code First。

由于dapper-dot-net只是IDbConnection的一些扩展,所以我可以直接使用它吗?

DbContext.Database.Connection 

如何使用dapper-dot-net?我已经测试它可以正常工作。但是,我不确定这是否是正确的使用方式?特别是当我以这种方式使用时,Entity Framework是否仍然会对性能产生一些负面影响?

4个回答

19

在某些情况下,使用Dapper可以显著提高性能。

你可以与Dapper共享EF连接。但是(虽然不太可能成为问题),你应该注意并发问题(例如由于尝试将多个数据读取器关联到同一连接而导致的问题)。

如果你遇到这样的问题,可以选择给Dapper一个使用连接字符串(DbContext.Database.Connection.ConnectionString)的新连接,而不是共享连接。


3
尽管真实,但在大多数情况下这不太可能成为问题——线程肯定不太可能是一个因素,因此主要存在的风险是存在一个打开的数据读取器——这很容易发现(错误并不是非常微妙)。 - Marc Gravell
5
@MarcGravell 我会相信你的话 :) 但是,总是给Dapper自己建立连接(而不是使用EF的连接)会不会更安全一些呢?还是这样做有什么劣势吗? - Eren Ersönmez

13

是的,您可以这样使用它。由于Dapper仅适用于扩展方法,因此您可以将其用于代码的性能敏感区域。您可以继续在代码的其他区域中使用EF。仍使用EF的查询速度可能不会很快,但至少使用Dapper的查询将更快。


2
根据丰富的经验,所谓“更快”,指的是“快两到三个数量级”。我曾经在Entity Framework中优化了一个查询,它需要10分钟才能完成。但是,将其转换为Dapper后,即使查询的大小和复杂度增加了20倍,它也只需要一小部分秒钟就能完成:https://dev59.com/hGox5IYBdhLWcg3wNRxj#30080951。 - Contango
@Contango 这是使用 EF 的 AsNoTracking 吗? - Ian Warburton
@Ian Warburton 不确定 - 但无论如何,Dapper的性能都远胜于Entity Framework。话虽如此,Dapper主要面向读取数据,因此如果您想将数据写入数据库,那么我猜Entity Framework可能会工作,如果一个人满意支持可能有10个并发用户的低效、平庸的性能的话。 - Contango
@Contango 为什么只有十个? - Ian Warburton
1
@Ian 我还没有看到任何Entity Framework的例子比设计为快速和高效(即在某些给定硬件上使用您选择的语言时,接近合理SQL查询的理论最大速度的50%内)的数据访问层慢10倍以下。它可能适用于10或50个用户-但是对于100个用户来说会很困难,肯定无法处理1000或10000个用户。当我谈论“用户”时,我更多地暗示了同时将一些负载放在数据库上的用户。 - Contango

2
我认为你需要重新考虑这个问题。通过更改ORM来提高性能并不是一种好的技术。虽然Dapper比EF更快更轻巧,但这并不意味着为了提高应用程序的速度而使用Dapper就更好。如果你遇到性能问题,EF已经足够强大,可以处理整个数据层,你需要引入新功能,如缓存或NoSQL数据库。
你必须看到,从EF切换到Dapper会为你节省多少时间?缓存可以为你的应用程序带来多少速度?
添加Dapper还有其他费用:当你需要保存和更新时,如何管理事务?你如何解决回滚的情况?单元工作和存储库模式又该怎么办?
在决定选择Dapper之前,这些都是你必须考虑的因素。

-1
Yes, you can use Dapper and Entity Framework in the same project, and here's a simple example of how you can do that. In this example, we'll use Entity Framework to perform most of the data access operations and Dapper for a specific scenario where fine-grained control over SQL is needed.
1)Set up Entity Framework in your project by creating a DbContext and defining your models. You can use Entity Framework to handle CRUD operations, data modeling, and migrations. Here's an example:
using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    // Define other DbSet properties for your entities

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Configure your database connection here
    }
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}


Use Entity Framework for most of your data access. For example, to retrieve users:

using (var context = new MyDbContext())
{
    var users = context.Users.ToList();
    // Perform other Entity Framework operations
}

When you need to use Dapper for a specific query, you can do so as follows. You'll need to have Dapper installed via NuGet:


using Dapper;
using System.Data;
using System.Data.SqlClient;



string connectionString = "your_connection_string_here";

using (IDbConnection dbConnection = new SqlConnection(connectionString))
{
    dbConnection.Open();

    // Define your SQL query
    string sqlQuery = "SELECT * FROM Users WHERE Name = @Name";

    // Use Dapper to execute the query and map the result to a strongly-typed object
    var user = dbConnection.QueryFirstOrDefault<User>(sqlQuery, new { Name = "John" });

    // Now you have a user object from Dapper
    // You can use it alongside your Entity Framework data
}

In this example, we have used Entity Framework for general data access operations and used Dapper for a specific scenario where we needed to execute a custom SQL query. Be sure to install the required packages and set up your database connection appropriately for both Entity Framework and Dapper to work together in your project.

问题不在于如何使用Dapper,而是“我能否只使用DbContext.Database.Connection”。这个问题已经得到了充分的回答。没有必要提供一个新的(格式混乱、难以阅读的)答案。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接