小巧的插入或更新? [英] Dapper insert or Update?

查看:111
本文介绍了小巧的插入或更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有开始使用Dapper,但是今天我在研究批量插入/更新时偶然发现了它。目前,我使用的是EF6,但我希望将来能使用Dapper。对于此应用程序,可能会有约15,000条记录,但我还有其他应用程序可能有约10万条记录。

I haven't started using Dapper just yet but just stumbled across it today as I was researching about bulk insert/updates. Currently I'm using EF6 but I would like to look at using Dapper in the future of my bulk stuff. For this app there could be ~15k records but I have other apps that could be ~100k records.

我正在尝试研究一种可以转换以下内容的方法EF代码导入Dapper。它所做的只是从文件中读取一条记录,查看该雇员是否在数据库中,如果存在,它将使用文件中的值更新属性,如果不存在,则使用文件中的值创建一个新对象。

I'm trying to research a way that I could convert the following EF code into Dapper. All it does is reads in a record from a file, look to see if that employee exists in the DB, if so it updates the properties with the values from the file and if not it creates a new object with the values from the file.

环顾四周时,我找不到任何示例。我所能找到的只是如何进行简单的插入或更新。我没有真正找到批量插入/更新的好例子。我很可能还不了解如何使用Dapper。

I couldn't find any examples when I was looking around. All I could find was how to do a simple insert or an update. I didn't really find a good examples of bulk insert/update. It's very possible I'm just not understanding how to use Dapper just yet.

我该如何使用Dapper?

How would I do this with Dapper?

int count = 1;
using (ctx = new DataContext())
{
    ctx.Configuration.AutoDetectChangesEnabled = false;      
    ctx.Configuration.ValidateOnSaveEnabled = false;

    while ((record = srFile.ReadLine()) != null)
    {
        int employeeId = int.Parse(record.Substring(2, 8));

        bio_employee employee = ctx.bio_employee.FirstOrDefault(e => e.emp_id == employeeId);

        if (employee != null)
        {
            SetEmployeeData(employee, record);

            ctx.Entry(employee).State = System.Data.Entity.EntityState.Modified;
        }
        else
        {
            employee = new bio_employee();
            employee.emp_id = employeeId;

            SetEmployeeData(employee, record);

            ctx.bio_employee.Add(employee);
        }


        if (count % batchSize == 0)
        {
            ctx.SaveChanges();
            ctx.Dispose();
            ctx = new DataContext();
        }

        count++;
    }
    ctx.SaveChanges();      //save any remaining
}


推荐答案

Dapper提供了多种查询数据的方法,但是除了使用通常没有ORM的命令外,没有其他方法可以执行保存操作。

Dapper provides multiple methods to query data but none to perform saving operations other than using a command like you normally do without an ORM.

很多第三方库都涵盖了这种情况对于Dapper:

A lot of third party libraries cover however this scenario for Dapper:

  • Dapper Plus (Recommended)
  • Dapper Contrib
  • Dapper Extensions
  • Dapper FastCRUD
  • Dapper SimpleCRUD

免责声明:我是m项目的所有者 Dapper Plus

Disclaimer: I'm the owner of the project Dapper Plus

Dapper Plus是到目前为止最快的库,它提供:BulkInsert,BulkDelete,BulkUpdate和BulkMerge操作。它可以轻松支持具有数百万条记录的方案。

Dapper Plus is by far the fastest library by providing: BulkInsert, BulkDelete, BulkUpdate, and BulkMerge operations. It can easily support scenarios with millions of records.

// CONFIGURE & MAP entity
DapperPlusManager.Entity<Employee>()
                 .Table("Employee")
                 .Identity(x => x.EmployeeID);

// SAVE entity
connection.BulkMerge(employeeList);

编辑:回答子问题


是DapperPlus中的.BulkMerge在做Upsert

Is the .BulkMerge in your DapperPlus doing an Upsert

是的,BulkMerge是

Yes, BulkMerge is an upsert operation.

您还可以使用映射键为同一实体指定多个映射。

You can also specify more than one mapping for the same entity by using a mapping key.

// Using key from database (x => x.EmployeeID)
DapperPlusManager.Entity<Employee>()
                 .Table("Employee");

connection.BulkInsert(employees);

// Using custom key
DapperPlusManager.Entity<Employee>("customKey")
                 .Table("Employee")
                 .Key(x => x.Code);

connection.BulkInsert("customKey", employees);

这篇关于小巧的插入或更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆