Entity Framework core (3+) has been improved significantly in this area. When there are multiple inserts that have the same shape, for Sql Server it will generate
MERGE statements rather than separate
For example, adding 10 new products:
DECLARE @inserted0 TABLE ([Id] int, [_Position] [int]); MERGE [Products] USING ( VALUES (@p0, @p1, @p2, @p3, @p4, 0), (@p5, @p6, @p7, @p8, @p9, 1), (@p10, @p11, @p12, @p13, @p14, 2), (@p15, @p16, @p17, @p18, @p19, 3), (@p20, @p21, @p22, @p23, @p24, 4), (@p25, @p26, @p27, @p28, @p29, 5), (@p30, @p31, @p32, @p33, @p34, 6), (@p35, @p36, @p37, @p38, @p39, 7), (@p40, @p41, @p42, @p43, @p44, 8), (@p45, @p46, @p47, @p48, @p49, 9)) AS i ([Image], [ProductName], [QuantityPerUnit], [StartDate], [UnitPrice], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Image], [ProductName], [QuantityPerUnit], [StartDate], [UnitPrice]) VALUES (i.[Image], i.[ProductName], i.[QuantityPerUnit], i.[StartDate], i.[UnitPrice]) OUTPUT INSERTED.[Id], i._Position INTO @inserted0; SELECT [t].[Id], [t].[RowVersion] FROM [Products] t INNER JOIN @inserted0 i ON ([t].[Id] = [i].[Id]) ORDER BY [i].[_Position];
SELECT query is for feeding back the generated
RowVersion values into the C# objects.
When there are large numbers of inserts, EF won't hit the maximum parameter threshold, it just generates multiple blocks of
It's not the same as an insert statement with value constructors, but still a lot better than countless separate inserts.
- How add multiple rows with one Insert query in EF
- Insert multiple rows to a table with 1 hit to the database
- EF Code First Insert Objects with One to Many gives exception."Store update, insert, or delete statement affected an unexpected number of rows (0)."
- Remove multiple rows from table with a LINQ query
- Multiple levels with theninclude not returning all rows unless I query the detail
- Convert multiple rows into a single row with multiple columns using LINQ query
- Trying to insert multiple rows ASP Web API with Postman in JSON
- Selecting multiple columns with linq query and lambda expression
- Linq query with multiple Contains/Any for RavenDB
- linq to sql query with multiple where parameters
- Deleting multiple records with Entity Framework using a single LINQ query
- LINQ query with a WHERE clause with multiple conditions
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Linq query with multiple OrderBy statements added in a loop
- LinQ query with multiple tables and extracting data
- LINQ query with multiple aggregates
- How can I create a dynamic LINQ query in C# with possible multiple group by clauses?
- Entity Framework Core LINQ query with multiple where clauses
- LINQ Filter query with multiple optional parameters
- C# Linq Query with Multiple Joins with an await
- Linq query with multiple subqueries
- how to delete multiple rows of data with linq to EF using DbContext
- LINQ to Objects: Query with multiple LIKE conditions (OR or AND) possible?
- Select multiple rows in order based on list of Ids with LINQ
- Query multiple tables with LINQ, grab results to dictionary
- Linq query with multiple count
- Convert linq query expression with multiple froms into extension method syntax
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Linq query and multiple where clauses with OR only first condition gets evaluated
- Linq Query With Multiple Joins Not Giving Correct Results
- InvalidCastException - passing null values to stored procedure
- Return results from a method returning values of a query in C#?
- How can I clean up this LINQ Query (SelectMany)?
- How to equate `var` variable to another query
- How to Pass dynamic condition in where clause in LINQ?
- Null reference in lambda max count
- DataTable - Linq - select distinct rows based on grouping
- Looking for non-matched items in a collection via Linq
- Using Func<> in Entity Framework Query
- Convert xls file to xml file using c#
- Linq Help. int.Contains and int == iqueryable doesn't work
- linq to entities changing database connection string in code
- LinqPad Not Returning Results With C# Statements
- Retrieve nullable object using EF CORE
- Compare two datatables and delete duplicates (without join)
- view model returning list of data in linq
- XML Sort to retain root data
- Orderby() not ordering numbers correctly c#
- Return IGrouping of anonymous IEnumerable to present on DataGrid
- Dynamically cast object to a type, when type is unknown at compile time