-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ef core generates a query for each row after AddRange() then SaveChanges() #21544
Comments
@amro93 although a separate INSERT is sent for each added row, these INSERTs are batched in a single round-trip, providing good performance. It's also necessary to have separate INSERTs in order to get any database-generated values (e.g. identity) back from the database, and populate them in the inserted entities. Do you have a particular problem or alternative implementation in mind here? |
@roji thank you for the fast response, its OK if the query is sent on a single round trip with multiple inserts, but I'd expect the logger to log this row "info: Microsoft.EntityFrameworkCore.Database.Command[20101]" once, which indicates that the transaction has been started. however the next lines of the code will be translated into the query below which make sense for a single round trip info: Microsoft.EntityFrameworkCore.Database.Command[20101] SET NOCOUNT ON;
|
I wrote a unit test which simulates the same exact code and it generated the expected query successfully unit test
Result query
|
When I added large number of rows 1000+ ef core generated a huge query for a single round trip. |
workflowStateRepository.CreateMany(wfStates);
await workflowStateRepository.SaveAsync();
public virtual void CreateMany(IEnumerable entities)
{
if (entities is null)
{
throw new ArgumentNullException(nameof(entities));
}
SetCreateEntityDetails(ref entities);
Set.AddRange(entities);
}
result query:
Got Exceptions? Include both the message and the stack trace
For
dotnet ef
and PMC, share the --verbose output-->
Further technical details
EF Core version: 3.1.5
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered: