private async Task RunDeleteAsync(bool isBulkOperation) { using (var context = new TestContext(ContextUtil.GetOptions())) { var entities = AllItemsQuery(context).ToList(); // ItemHistories will also be deleted because of Relationship - ItemId (Delete Rule: Cascade) if (isBulkOperation) { await context.BulkDeleteAsync(entities); } else { context.Items.RemoveRange(entities); await context.SaveChangesAsync(); } } using (var context = new TestContext(ContextUtil.GetOptions())) { int entitiesCount = ItemsCountQuery(context); Item lastEntity = LastItemQuery(context); Assert.Equal(0, entitiesCount); Assert.Null(lastEntity); } using (var context = new TestContext(ContextUtil.GetOptions())) { // Resets AutoIncrement context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('dbo.[" + nameof(Item) + "]', RESEED, 0);"); //context.Database.ExecuteSqlCommand($"TRUNCATE TABLE {nameof(Item)};"); // can NOT work when there is ForeignKey - ItemHistoryId } }
private async Task RunDeleteAsync(bool isBulkOperation, DbServer databaseType) { using (var context = new TestContext(ContextUtil.GetOptions())) { var entities = AllItemsQuery(context).ToList(); // ItemHistories will also be deleted because of Relationship - ItemId (Delete Rule: Cascade) if (isBulkOperation) { var bulkConfig = new BulkConfig() { CalculateStats = true }; await context.BulkDeleteAsync(entities, bulkConfig); if (databaseType == DbServer.SqlServer) { Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberInserted); Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberUpdated); Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo.StatsNumberDeleted); } } else { context.Items.RemoveRange(entities); await context.SaveChangesAsync(); } } using (var context = new TestContext(ContextUtil.GetOptions())) { //int entitiesCount = ItemsCountQuery(context); int entitiesCount = await context.Items.CountAsync(); //Item lastEntity = LastItemQuery(context); Item lastEntity = context.Items.OrderByDescending(a => a.ItemId).FirstOrDefault(); Assert.Equal(0, entitiesCount); Assert.Null(lastEntity); } using (var context = new TestContext(ContextUtil.GetOptions())) { if (databaseType == DbServer.SqlServer) { await context.Database.ExecuteSqlRawAsync("DBCC CHECKIDENT('[dbo].[Item]', RESEED, 0);").ConfigureAwait(false); } if (databaseType == DbServer.Sqlite) { await context.Database.ExecuteSqlRawAsync("DELETE FROM sqlite_sequence WHERE name = 'Item';").ConfigureAwait(false); } } }
private async Task RunDeleteAsync(bool isBulk, DbServer dbServer) { using var context = new TestContext(ContextUtil.GetOptions()); var entities = AllItemsQuery(context).ToList(); // ItemHistories will also be deleted because of Relationship - ItemId (Delete Rule: Cascade) if (isBulk) { var bulkConfig = new BulkConfig() { CalculateStats = true }; await context.BulkDeleteAsync(entities, bulkConfig); if (dbServer == DbServer.SQLServer) { Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberInserted); Assert.Equal(0, bulkConfig.StatsInfo.StatsNumberUpdated); Assert.Equal(EntitiesNumber / 2, bulkConfig.StatsInfo.StatsNumberDeleted); } } else { context.Items.RemoveRange(entities); await context.SaveChangesAsync(); } // TEST int entitiesCount = await context.Items.CountAsync(); Item lastEntity = context.Items.OrderByDescending(a => a.ItemId).FirstOrDefault(); Assert.Equal(0, entitiesCount); Assert.Null(lastEntity); // RESET AutoIncrement string deleteTableSql = dbServer switch { DbServer.SQLServer => $"DBCC CHECKIDENT('[dbo].[{nameof(Item)}]', RESEED, 0);", DbServer.SQLite => $"DELETE FROM sqlite_sequence WHERE name = '{nameof(Item)}';", _ => throw new ArgumentException($"Unknown database type: '{dbServer}'.", nameof(dbServer)), }; await context.Database.ExecuteSqlRawAsync(deleteTableSql).ConfigureAwait(false); }
internal async Task RunDeleteAllAsync(DbServer databaseType) { using (var context = new TestContext(ContextUtil.GetOptions())) { await context.Items.AddAsync(new Item { }); // used for initial add so that after RESEED it starts from 1, not 0 await context.SaveChangesAsync(); //await context.Items.BatchDeleteAsync(); // TODO: Use after BatchDelete gets implemented for v3.0 await context.BulkDeleteAsync(context.Items.ToList()); if (databaseType == DbServer.SqlServer) { await context.Database.ExecuteSqlRawAsync("DBCC CHECKIDENT('[dbo].[Item]', RESEED, 0);").ConfigureAwait(false); } if (databaseType == DbServer.Sqlite) { await context.Database.ExecuteSqlRawAsync("DELETE FROM sqlite_sequence WHERE name = 'Item';").ConfigureAwait(false); } } }
internal async Task RunDeleteAllAsync(DbServer dbServer) { using var context = new TestContext(ContextUtil.GetOptions()); await context.Items.AddAsync(new Item { }); // used for initial add so that after RESEED it starts from 1, not 0 await context.SaveChangesAsync(); await context.Items.BatchDeleteAsync(); await context.BulkDeleteAsync(context.Items.ToList()); // RESET AutoIncrement string deleteTableSql = dbServer switch { DbServer.SqlServer => $"DBCC CHECKIDENT('[dbo].[{nameof(Item)}]', RESEED, 0);", DbServer.Sqlite => $"DELETE FROM sqlite_sequence WHERE name = '{nameof(Item)}';", _ => throw new ArgumentException($"Unknown database type: '{dbServer}'.", nameof(dbServer)), }; context.Database.ExecuteSqlRaw(deleteTableSql); }
private async Task RunDeleteAsync(bool isBulkOperation, DbServer databaseType) { using (var context = new TestContext(ContextUtil.GetOptions())) { var entities = AllItemsQuery(context).ToList(); // ItemHistories will also be deleted because of Relationship - ItemId (Delete Rule: Cascade) if (isBulkOperation) { await context.BulkDeleteAsync(entities); } else { context.Items.RemoveRange(entities); await context.SaveChangesAsync(); } } using (var context = new TestContext(ContextUtil.GetOptions())) { int entitiesCount = ItemsCountQuery(context); Item lastEntity = LastItemQuery(context); Assert.Equal(0, entitiesCount); Assert.Null(lastEntity); } using (var context = new TestContext(ContextUtil.GetOptions())) { if (databaseType == DbServer.SqlServer) { await context.Database.ExecuteSqlCommandAsync("DBCC CHECKIDENT('[dbo].[Item]', RESEED, 0);").ConfigureAwait(false); } if (databaseType == DbServer.Sqlite) { await context.Database.ExecuteSqlCommandAsync("DELETE FROM sqlite_sequence WHERE name = 'Item';").ConfigureAwait(false); } } }