Skip to content

Commit

Permalink
✨ 68 enhanced bulk updated delete (#76)
Browse files Browse the repository at this point in the history
* Added EnhancedBulkUpdatedAndDelete.csproj

* Rename project

* Got enhanced bulk update working.

* Updated readme
  • Loading branch information
danielmackay authored Dec 27, 2023
1 parent 05d0214 commit 143cb4b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions EfCoreSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrimitiveCollections", "Pri
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnhancedJsonColumns", "EnhancedJsonColumns\EnhancedJsonColumns.csproj", "{EB8182A7-259B-4EE3-A00D-FEB6E3C1C0E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnhancedBulkUpdateAndDelete", "EnhancedBulkUpdateAndDelete\EnhancedBulkUpdateAndDelete.csproj", "{89031EE1-2782-404B-B06E-FCFEE98BAC1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -176,6 +178,10 @@ Global
{EB8182A7-259B-4EE3-A00D-FEB6E3C1C0E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB8182A7-259B-4EE3-A00D-FEB6E3C1C0E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB8182A7-259B-4EE3-A00D-FEB6E3C1C0E2}.Release|Any CPU.Build.0 = Release|Any CPU
{89031EE1-2782-404B-B06E-FCFEE98BAC1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89031EE1-2782-404B-B06E-FCFEE98BAC1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89031EE1-2782-404B-B06E-FCFEE98BAC1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89031EE1-2782-404B-B06E-FCFEE98BAC1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
20 changes: 20 additions & 0 deletions EnhancedBulkUpdateAndDelete/ApplicationDbContext.cs
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 EnhancedBulkUpdateAndDelete/EnhancedBulkUpdateAndDelete.csproj
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>
39 changes: 39 additions & 0 deletions EnhancedBulkUpdateAndDelete/Product.cs
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
}
27 changes: 27 additions & 0 deletions EnhancedBulkUpdateAndDelete/Program.cs
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();
11 changes: 11 additions & 0 deletions EnhancedBulkUpdateAndDelete/readme.md
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)

0 comments on commit 143cb4b

Please sign in to comment.