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 来指定自定义键 (Online Example):

await _db.Enotes.BulkMergeAsync(orderEnotes,operation =>
{
    operation.ColumnPrimaryKeyExpression = prop => new { prop.Entity,prop.EnoteType,prop.KeyValue1 };
});

此外,

选项 InsertIfNotExists 仅适用于 BulkInsert,默认情况下 InsertKeepIdentity 已经为 false。