ویژگی Batching در EF Core
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۶/۱۵ ۱۳:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
optionsBuilder.UseSqlServer( @"Server=(localdb)\mssqllocaldb;Database=Demo.Batching;Trusted_Connection=True;", options => options.MaxBatchSize(1) );
public class SqlServerModificationCommandBatch : AffectedCountModificationCommandBatch
{
private const int DefaultNetworkPacketSizeBytes = 4096;
private const int MaxScriptLength = 65536 * DefaultNetworkPacketSizeBytes / 2;
private const int MaxParameterCount = 2100;
private const int MaxRowCount = 1000; public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Demo.Batching;Trusted_Connection=True;"/*,
options => options.MaxBatchSize(2)*/
);
optionsBuilder.EnableSensitiveDataLogging();
}
} using (var db = new BloggingContext())
{
db.GetService<ILoggerFactory>().AddProvider(new MyLoggerProvider());
// Modify some existing blogs
var existing = db.Blogs.ToArray();
existing[0].Url = "http://sample.com/blogs/dogs";
existing[1].Url = "http://sample.com/blogs/cats";
// Insert some new blogs
db.Blogs.Add(new Blog { Name = "The Horse Blog", Url = "http://sample.com/blogs/horses" });
db.Blogs.Add(new Blog { Name = "The Snake Blog", Url = "http://sample.com/blogs/snakes" });
db.Blogs.Add(new Blog { Name = "The Fish Blog", Url = "http://sample.com/blogs/fish" });
db.Blogs.Add(new Blog { Name = "The Koala Blog", Url = "http://sample.com/blogs/koalas" });
db.Blogs.Add(new Blog { Name = "The Parrot Blog", Url = "http://sample.com/blogs/parrots" });
db.Blogs.Add(new Blog { Name = "The Kangaroo Blog", Url = "http://sample.com/blogs/kangaroos" });
db.SaveChanges();
} Executed DbCommand (41ms) [Parameters=[@p1='57', @p0='http://sample.com/blogs/dogs' (Size = 4000), @p3='58', @p2='http://sample.com/blogs/cats' (Size = 4000), @p4='The Horse Blog' (Size = 4000), @p5='http://sample.com/blogs/horses' (Size = 4000), @p6='The Snake Blog' (Size = 4000), @p7='http://sample.com/blogs/snakes' (Size = 4000), @p8='The Fish Blog' (Size = 4000), @p9='http://sample.com/blogs/fish' (Size = 4000), @p10='The Koala Blog' (Size = 4000), @p11='http://sample.com/blogs/koalas' (Size = 4000), @p12='The Parrot Blog' (Size = 4000), @p13='http://sample.com/blogs/parrots' (Size = 4000), @p14='The Kangaroo Blog' (Size = 4000), @p15='http://sample.com/blogs/kangaroos' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON; UPDATE [Blogs] SET [Url] = @p0 WHERE [BlogId] = @p1; SELECT @@ROWCOUNT; UPDATE [Blogs] SET [Url] = @p2 WHERE [BlogId] = @p3; SELECT @@ROWCOUNT; DECLARE @inserted2 TABLE ([BlogId] int, [_Position] [int]); MERGE [Blogs] USING ( VALUES (@p4, @p5, 0), (@p6, @p7, 1), (@p8, @p9, 2), (@p10, @p11, 3), (@p12, @p13, 4), (@p14, @p15, 5)) AS i ([Name], [Url], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Name], [Url]) VALUES (i.[Name], i.[Url]) OUTPUT INSERTED.[BlogId], i._Position INTO @inserted2; SELECT [t].[BlogId] FROM [Blogs] t INNER JOIN @inserted2 i ON ([t].[BlogId] = [i].[BlogId]) ORDER BY [i].[_Position];
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Demo.Batching;Trusted_Connection=True;",
options => options.MaxBatchSize(2)
);
optionsBuilder.EnableSensitiveDataLogging();
} Executed DbCommand (17ms) [Parameters=[@p1='65', @p0='http://sample.com/blogs/dogs' (Size = 4000), @p3='66', @p2='http://sample.com/blogs/cats' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON; UPDATE [Blogs] SET [Url] = @p0 WHERE [BlogId] = @p1; SELECT @@ROWCOUNT; UPDATE [Blogs] SET [Url] = @p2 WHERE [BlogId] = @p3; SELECT @@ROWCOUNT; Executed DbCommand (18ms) [Parameters=[@p0='The Horse Blog' (Size = 4000), @p1='http://sample.com/blogs/horses' (Size = 4000), @p2='The Snake Blog' (Size = 4000), @p3='http://sample.com/blogs/snakes' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON; DECLARE @inserted0 TABLE ([BlogId] int, [_Position] [int]); MERGE [Blogs] USING ( VALUES (@p0, @p1, 0), (@p2, @p3, 1)) AS i ([Name], [Url], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Name], [Url]) VALUES (i.[Name], i.[Url]) OUTPUT INSERTED.[BlogId], i._Position INTO @inserted0; SELECT [t].[BlogId] FROM [Blogs] t INNER JOIN @inserted0 i ON ([t].[BlogId] = [i].[BlogId]) ORDER BY [i].[_Position]; Executed DbCommand (34ms) [Parameters=[@p0='The Fish Blog' (Size = 4000), @p1='http://sample.com/blogs/fish' (Size = 4000), @p2='The Koala Blog' (Size = 4000), @p3='http://sample.com/blogs/koalas' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON; DECLARE @inserted0 TABLE ([BlogId] int, [_Position] [int]); MERGE [Blogs] USING ( VALUES (@p0, @p1, 0), (@p2, @p3, 1)) AS i ([Name], [Url], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Name], [Url]) VALUES (i.[Name], i.[Url]) OUTPUT INSERTED.[BlogId], i._Position INTO @inserted0; SELECT [t].[BlogId] FROM [Blogs] t INNER JOIN @inserted0 i ON ([t].[BlogId] = [i].[BlogId]) ORDER BY [i].[_Position]; Executed DbCommand (15ms) [Parameters=[@p0='The Parrot Blog' (Size = 4000), @p1='http://sample.com/blogs/parrots' (Size = 4000), @p2='The Kangaroo Blog' (Size = 4000), @p3='http://sample.com/blogs/kangaroos' (Size = 4000)], CommandType='Text', CommandTimeout='30'] SET NOCOUNT ON; DECLARE @inserted0 TABLE ([BlogId] int, [_Position] [int]); MERGE [Blogs] USING ( VALUES (@p0, @p1, 0), (@p2, @p3, 1)) AS i ([Name], [Url], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Name], [Url]) VALUES (i.[Name], i.[Url]) OUTPUT INSERTED.[BlogId], i._Position INTO @inserted0; SELECT [t].[BlogId] FROM [Blogs] t INNER JOIN @inserted0 i ON ([t].[BlogId] = [i].[BlogId]) ORDER BY [i].[_Position];
UPDATE [dbo].[Blogs] SET [Url] = @0 WHERE ([BlogId] = @1) -- @0: 'http://sample.com/blogs/dogs' (Type = String, Size = -1) -- @1: '1' (Type = Int32) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 19 ms with result: 1 UPDATE [dbo].[Blogs] SET [Url] = @0 WHERE ([BlogId] = @1) -- @0: 'http://sample.com/blogs/cats' (Type = String, Size = -1) -- @1: '2' (Type = Int32) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 47 ms with result: 1 INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Horse Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/horses' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 31 ms with result: SqlDataReader INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Snake Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/snakes' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 73 ms with result: SqlDataReader INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Fish Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/fish' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 49 ms with result: SqlDataReader INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Koala Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/koalas' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 49 ms with result: SqlDataReader INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Parrot Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/parrots' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:41 ب.ظ +04:30 -- Completed in 26 ms with result: SqlDataReader INSERT [dbo].[Blogs]([Name], [Url]) VALUES (@0, @1) SELECT [BlogId] FROM [dbo].[Blogs] WHERE @@ROWCOUNT > 0 AND [BlogId] = scope_identity() -- @0: 'The Kangaroo Blog' (Type = String, Size = -1) -- @1: 'http://sample.com/blogs/kangaroos' (Type = String, Size = -1) -- Executing at 16/06/1396 02:31:42 ب.ظ +04:30 -- Completed in 38 ms with result: SqlDataReader