我可以将 dapper-dot-net 与实体框架一起使用吗?

问题描述 投票:0回答:4

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

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

DbContext.Database.Connection 

使用 dapper-dot-net?我测试它有效。但是,我不确定这是正确的使用方法吗?特别是,当我这样使用时,Entity Framework 还会有一些影响,可能会损害性能吗?

performance entity-framework dapper
4个回答
19
投票

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

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

如果您确实遇到此类问题,您可以选择使用连接字符串 (

DbContext.Database.Connection.ConnectionString
) 向 Dapper 提供新连接,而不是共享连接。


13
投票

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


2
投票

我认为你必须重新思考这个问题。通过更改 ORM 来提高性能并不是一个好技术。确实,dapper 比 EF 更快、更轻,但这并不意味着要提高应用程序的速度最好使用 dapper。 EF 足够强大,可以处理整个数据层,如果您受到性能影响,则必须引入新功能,例如缓存或无 sql 数据库。

您必须看到从 EF 更改为 dapper 将为您节省多少时间?以及缓存可以为您的应用程序带来多少速度。

添加 dapper 还有其他费用:当您必须保存和更新时,您将如何管理事务,您将如何解决回滚情况,工作单元和存储库模式又如何。

这些是您在决定选择 Dapper 之前必须检查的因素。


0
投票
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.
© www.soinside.com 2019 - 2024. All rights reserved.