-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 68 enhanced bulk updated delete (#76)
* Added EnhancedBulkUpdatedAndDelete.csproj * Rename project * Got enhanced bulk update working. * Updated readme
- Loading branch information
1 parent
05d0214
commit 143cb4b
Showing
6 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Common; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace EnhancedBulkUpdateAndDelete; | ||
|
||
public class ApplicationDbContext : DbContext | ||
{ | ||
public DbSet<Product> Products => Set<Product>(); | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder | ||
.UseSqlServer(DbConnectionFactory.Create("EnhancedBulkUpdateAndDelete")); | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
modelBuilder.Entity<Product>().OwnsOne(p => p.Color); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
EnhancedBulkUpdateAndDelete/EnhancedBulkUpdateAndDelete.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace EnhancedBulkUpdateAndDelete; | ||
|
||
public class Product | ||
{ | ||
public int Id { get; private set; } | ||
public required string Name { get; init; } | ||
|
||
public required Color Color { get; init; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"Id: {Id}, Name: {Name}, Color: {Color}"; | ||
} | ||
} | ||
|
||
public class Color | ||
{ | ||
public ColorCode Code { get; init; } | ||
public required string Name { get; init; } | ||
public int NumInStock { get; init; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"Code: {Code}, Name: {Name}, NumInStock: {NumInStock}"; | ||
} | ||
} | ||
|
||
public enum ColorCode | ||
{ | ||
Red = 1, | ||
Green, | ||
Blue, | ||
Yellow, | ||
Orange, | ||
Purple, | ||
Black, | ||
White, | ||
Brown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using EnhancedBulkUpdateAndDelete; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
Console.WriteLine("Enhanced Bulk Update and Delete Sample"); | ||
|
||
using var db = new ApplicationDbContext(); | ||
db.Database.EnsureDeleted(); | ||
db.Database.EnsureCreated(); | ||
|
||
var products = new List<Product> | ||
{ | ||
new() { Name = "Product 1", Color = new Color { Name = "Black", Code = ColorCode.Black, NumInStock = 10 }, }, | ||
new() { Name = "Product 2", Color = new Color { Name = "Red", Code = ColorCode.Red, NumInStock = 5 }, }, | ||
new() { Name = "Product 3", Color = new Color { Name = "White", Code = ColorCode.White, NumInStock = 2 }, }, | ||
}; | ||
|
||
db.Products.AddRange(products); | ||
db.SaveChanges(); | ||
|
||
db.Products | ||
.ExecuteUpdate(u => u.SetProperty(p => p.Color.NumInStock, 0)); | ||
|
||
var allProducts = db.Products.AsNoTracking().ToList(); | ||
Console.WriteLine("All Products"); | ||
allProducts.ForEach(Console.WriteLine); | ||
|
||
Console.ReadLine(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Enhanced Bulk Update and Delete | ||
|
||
EF Core has always required that Bulk Updates and Deletes be performed on a single table. However, when using Owned Entities, the data is in a single table, but EF Core still wouldn't allow you to perform a Bulk Update or Delete. | ||
|
||
## Use Cases | ||
|
||
- Bulk Update and Delete on Owned Entities | ||
|
||
## Resources | ||
|
||
- [EF Core Docs | Enhanced Bulk Updates and Deletes](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/whatsnew#better-executeupdate-and-executedelete) |