简体   繁体   English

实体框架核心批量扩展:BulkInsertOrUpdate

[英]Entity Framework core bulk Extensions: BulkInsertOrUpdate

I have requirement in entityfremawork core bulk extensions feature.我对 entityfremawork 核心批量扩展功能有要求。 when we are using _context.BulkInsertOrUpdate(Customers);当我们使用 _context.BulkInsertOrUpdate(Customers); I have not created any primary key on sql customer table.我没有在 sql 客户表上创建任何主键。 So i am trying to map target columns(Insert or update) externally like below所以我想像下面这样在外部尝试 map 目标列(插入或更新)

context.BulkInsertOrUpdate()
  .MatchTargetOn(x => x.CustomerId)
  .MatchTargetOn(x => x.CustomerName)
  .Commit(db.Database.Connection);

Is there any way in ef core bulk extensions ef核心批量扩展有什么办法吗

There is no solution if library do not support such matching.如果库不支持这种匹配,则没有解决方案。 So consider to change third party library to another or wait for feature implementation.因此考虑将第三方库更改为另一个库或等待功能实现。

BTW, I have created this bridge between linq2db and EF Core linq2db.EntityFrameworkCore , so if you have no other options, this one should work.顺便说一句,我已经在linq2dbEF Core linq2db.EntityFrameworkCore之间创建了这座桥,所以如果你没有其他选择,这个应该可以工作。

context.Customers
   .ToLinqToDBTable()
   .Merge()
   .Using(entities)
   .On((t, s) => t.CustomerId == s.CustomerId || t.CustomerName == s.CustomerName)
   .InsertWhenNotMatched()
   .UpdateWhenMatched()
   .Merge();

Check overloads of InsertWhenNotMatched and UpdateWhenMatched - here you can customize which fields should be inserted or updated even based on values from target table.检查InsertWhenNotMatchedUpdateWhenMatched的重载 - 在这里您可以自定义应该插入或更新哪些字段,甚至可以基于目标表中的值。

Disclaimer : I'm the owner of Entity Framework Extensions免责声明:我是实体框架扩展的所有者

This library is not free but covers easily scenarios such as custom keys.该库不是免费的,但可以轻松涵盖自定义键等场景。 The InsertOrUpdate is equivalent to BulkMerge . InsertOrUpdate相当于BulkMerge

Example:例子:

context.BulkMerge(list,  options => options.ColumnPrimaryKeyExpression = c => 
    new { c.CustomerId, c.CustomerName });

Here is an online example: https://dotnetfiddle.net/Lzbh2H这是一个在线示例: https://dotnetfiddle.net/Lzbh2H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM