.Net 6! Zack.EFCore.Batch can update, insert, and delete in batches with Entity Framework Core.

Zack Yang
2 min readDec 13, 2021

--

As we know, when updating or deleting data in Entity Framework Core, we need to query the data first and then update or delete the data one by one, which is very inefficient. Microsoft has plans for batch support of EF Core in .NET 7.
We can’t wait any more! You can use Zack.EFCore.Batch right now!
Zack.EFCore.Batch, which uses EFCore’s translation engine to translate expressions into SQL statements, so function calls written in C# such as dates and strings are automatically translated into the corresponding database dialect SQL. It also supports advanced EF Core features such as QueryFilter, custom Function, and ValueConverter.

URL:https://github.com/yangzhongke/Zack.EFCore.Batch
It contains the following new features:
1) It supports .Net 5 and .NET 6;
2) ValueConverter is supported when doing BulkInsert().
3) Modified The underlying implementation to completely solve The “The count of columns should be even” exception when two columns are equivalent when updating data.
Zack.EFCore.Batch can generate a single SQL statement for update and delete. It supports most of the EFCore expressions and advanced features such as QueryFilter, custom Function, and ValueConverter.
4)Two overloaded methods were added.Set(“Message”,”abc”);Set(b => b.PubTime, DateTime.Now);

Here’s how the new feature works:
1)The metadata type corresponding to the property of the entity class in EF Core is IProperty. ValueConverter of the property can be obtained through GetValueConverter() of the IProperty. So we simply call ValueConverter to transform the data before inserting it into the database.

2)In BatchUpdateBuilder, the Set related expression of assignment operation is concatenated into the column of Select to achieve the translation of the expression into SQL statement fragment. Some databases will ignore the column of the same expression when translating the column of the same expression, the “The count of columns should be even exception” was thrown.
The idea is to check the expressiones in advance. I wrote the ExpressionEqualityComparer to check the equality of two Expressions.

3)I implement the Set method using building an expression tree dynamically.
public BatchUpdateBuilder<TEntity> Set(string name,
object value)
{
var propInfo = typeof(TEntity).GetProperty(name);
Type propType = propInfo.PropertyType;//typeof of property

var pExpr = Expression.Parameter(typeof(TEntity));
Type tDelegate = typeof(Func<,>).MakeGenericType(typeof(TEntity),propType);

var nameExpr = Expression.Lambda(tDelegate,Expression.MakeMemberAccess(pExpr, propInfo), pExpr);
Expression valueExpr = Expression.Constant(value);
if (value!=null&&value.GetType()!= propType)
{
valueExpr = Expression.Convert(valueExpr, propType);
}
var valueLambdaExpr = Expression.Lambda(tDelegate, valueExpr, pExpr);
//…
}

URL:https://github.com/yangzhongke/Zack.EFCore.Batch

--

--

Zack Yang
Zack Yang

No responses yet