merge - Z.EntityFramework.Extensions.EFCore BulkMerge - 非键索引以更新大量记录
问题描述
我有需要更新大量记录的情况。然而,mytable 是非常动态的,它的主键是 DBGenerated。我想知道是否有人使用 EF Extensions 来完成基于其他字段的更新。我已经从他们的文档中尝试了以下内容,但它没有映射,只是重新插入了很多。我曾多次尝试修改选项,但没有太大成功。我需要将“更新键”映射为其他三列,保留原始 PK。有人能在保持速度的同时提出更好的路线吗?..我真的不想循环并一次手动更新每一个
https://bulk-operations.net/bulk-merge
await _db.Enotes.BulkMergeAsync(orderEnotes, operation =>
{
operation.AutoMapKeyExpression = prop => new { prop.Entity, prop.EnoteType, prop.KeyValue1 };
operation.InsertIfNotExists = true;
operation.InsertKeepIdentity = false;
});
班级
public class EnoteEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 EnoteID { get; set; }
public string Entity { get; set; }
public string EnoteType { get; set; }
public string KeyValue1 { get; set; }
public string KeyValue2 { get; set; }
Code removed for brevity...
解决方案
您当前使用该库Entity Framework Extensions
,但您正在查看Bulk Operations
文档,因此存在一些差异。
这是关于批量合并的正确文档:https ://entityframework-extensions.net/bulk-merge
正如您将发现的那样,您不应该使用AutoMapKeyExpression
而是ColumnPrimaryKeyExpression
指定自定义键(在线示例):
await _db.Enotes.BulkMergeAsync(orderEnotes, operation =>
{
operation.ColumnPrimaryKeyExpression = prop => new { prop.Entity, prop.EnoteType, prop.KeyValue1 };
});
此外,
该选项InsertIfNotExists
仅适用于BulkInsert
并且InsertKeepIdentity
默认情况下已经为 false。
推荐阅读
- c# - iText 7 从 Asp.Net WebApi 返回 Pdf
- python - 熊猫数据框从元素频率大于1的列创建唯一ID
- windows - 排序对象对 Get-EventLog 没有影响
- python - mpi 插槽不可用
- node.js - 如何处理运行时间超过 cron 间隔的 pm2 cron 作业?
- python - Pandas str.contains 超过一个列表?
- maven - 如何在提交/拉取请求后更新 maven 包
- haskell - 使用“否则”时如何获得“非详尽模式”异常
- mongodb - 使用 Java 驱动程序 3.8 的 ColdFusion 和 MongoDB 4
- python - 在 GMSH 物理线获取 FaceVariable