diff --git a/vs4win/Chapter10/WorkingWithEFCore/Program.Modifications.cs b/vs4win/Chapter10/WorkingWithEFCore/Program.Modifications.cs index e10ba19..18a2234 100644 --- a/vs4win/Chapter10/WorkingWithEFCore/Program.Modifications.cs +++ b/vs4win/Chapter10/WorkingWithEFCore/Program.Modifications.cs @@ -1,10 +1,11 @@ -using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry +using Microsoft.EntityFrameworkCore; // ExecuteUpdate, ExecuteDelete +using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry using Microsoft.EntityFrameworkCore.Storage; // IDbContextTransaction -using Packt.Shared; +using Packt.Shared; // Northwind, Product partial class Program { - static void ListProducts(int? productIdToHighlight = null) + static void ListProducts(int[]? productIdsToHighlight = null) { using (Northwind db = new()) { @@ -21,7 +22,8 @@ partial class Program { ConsoleColor previousColor = ForegroundColor; - if (productIdToHighlight == p.ProductId) + if ((productIdsToHighlight is not null) && + productIdsToHighlight.Contains(p.ProductId)) { ForegroundColor = ConsoleColor.Green; } @@ -39,6 +41,8 @@ partial class Program { using (Northwind db = new()) { + if (db.Products is null) return (0, 0); + Product p = new() { CategoryId = categoryId, @@ -64,7 +68,9 @@ partial class Program { using (Northwind db = new()) { - // get first product whose name starts with name + if (db.Products is null) return (0, 0); + + // Get the first product whose name starts with name. Product updateProduct = db.Products.First( p => p.ProductName.StartsWith(productNameStartsWith)); @@ -86,15 +92,16 @@ partial class Program arg0: t.GetDbTransaction().IsolationLevel); IQueryable? products = db.Products?.Where( - p => p.ProductName.StartsWith(productNameStartsWith)); + p => p.ProductName.StartsWith(productNameStartsWith)); - if (products is null) + if ((products is null) || (!products.Any())) { WriteLine("No products found to delete."); return 0; } else { + if (db.Products is null) return 0; db.Products.RemoveRange(products); } @@ -104,4 +111,47 @@ partial class Program } } } + + static (int affected, int[]? productIds) IncreaseProductPricesBetter( + string productNameStartsWith, decimal amount) + { + using (Northwind db = new()) + { + if (db.Products is null) return (0, null); + + // Get products whose name starts with name. + IQueryable? products = db.Products.Where( + p => p.ProductName.StartsWith(productNameStartsWith)); + + int affected = products.ExecuteUpdate(s => s.SetProperty( + p => p.Cost, // Property selector lambda expression. + p => p.Cost + amount)); // Value to update to lambda expression. + + int[] productIds = products.Select(p => p.ProductId).ToArray(); + + return (affected, productIds); + } + } + + static int DeleteProductsBetter(string productNameStartsWith) + { + using (Northwind db = new()) + { + int affected = 0; + + IQueryable? products = db.Products?.Where( + p => p.ProductName.StartsWith(productNameStartsWith)); + + if ((products is null) || (!products.Any())) + { + WriteLine("No products found to delete."); + return 0; + } + else + { + affected = products.ExecuteDelete(); + } + return affected; + } + } } \ No newline at end of file diff --git a/vs4win/Chapter10/WorkingWithEFCore/Program.cs b/vs4win/Chapter10/WorkingWithEFCore/Program.cs index 650cf0e..6e6d82d 100644 --- a/vs4win/Chapter10/WorkingWithEFCore/Program.cs +++ b/vs4win/Chapter10/WorkingWithEFCore/Program.cs @@ -1,42 +1,59 @@ using Packt.Shared; -/* + Northwind db = new(); WriteLine($"Provider: {db.Database.ProviderName}"); -*/ + // QueryingCategories(); -//FilteredIncludes(); +// FilteredIncludes(); -//QueryingProducts(); +// QueryingProducts(); //QueryingWithLike(); //GetRandomProduct(); +/* var resultAdd = AddProduct(categoryId: 6, productName: "Bob's Burgers", price: 500M); if (resultAdd.affected == 1) { - WriteLine("Add product successful."); + WriteLine($"Add product successful with ID: {resultAdd.productId}."); } -ListProducts(productIdToHighlight: resultAdd.productId); +ListProducts(productIdsToHighlight: new[] { resultAdd.productId }); +*/ +/* var resultUpdate = IncreaseProductPrice( productNameStartsWith: "Bob", amount: 20M); if (resultUpdate.affected == 1) +{ + WriteLine($"Increase price success for ID: {resultUpdate.productId}."); +} + +ListProducts(productIdsToHighlight: new[] { resultUpdate.productId }); +*/ + +/* +var resultUpdateBetter = IncreaseProductPricesBetter( + productNameStartsWith: "Bob", amount: 20M); + +if (resultUpdateBetter.affected > 0) { WriteLine("Increase product price successful."); } -ListProducts(productIdToHighlight: resultUpdate.productId); +ListProducts(productIdsToHighlight: resultUpdateBetter.productIds); +*/ +/* WriteLine("About to delete all products whose name starts with Bob."); -Write("Press Enter to continue: "); +Write("Press Enter to continue or any other key to exit: "); if (ReadKey(intercept: true).Key == ConsoleKey.Enter) { int deleted = DeleteProducts(productNameStartsWith: "Bob"); @@ -45,4 +62,19 @@ if (ReadKey(intercept: true).Key == ConsoleKey.Enter) else { WriteLine("Delete was canceled."); -} \ No newline at end of file +} +*/ + +/* +WriteLine("About to delete all products whose name starts with Bob."); +Write("Press Enter to continue or any other key to exit: "); +if (ReadKey(intercept: true).Key == ConsoleKey.Enter) +{ + int deleted = DeleteProductsBetter(productNameStartsWith: "Bob"); + WriteLine($"{deleted} product(s) were deleted."); +} +else +{ + WriteLine("Delete was canceled."); +} +*/ \ No newline at end of file diff --git a/vs4win/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj b/vs4win/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj index a90f1ab..a594e51 100644 --- a/vs4win/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj +++ b/vs4win/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj @@ -7,20 +7,20 @@ enable - - - + + + - - - - - + + + + + - - - Always - - + + + PreserveNewest + + diff --git a/vscode/Chapter10/WorkingWithEFCore/Program.Modifications.cs b/vscode/Chapter10/WorkingWithEFCore/Program.Modifications.cs index e10ba19..18a2234 100644 --- a/vscode/Chapter10/WorkingWithEFCore/Program.Modifications.cs +++ b/vscode/Chapter10/WorkingWithEFCore/Program.Modifications.cs @@ -1,10 +1,11 @@ -using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry +using Microsoft.EntityFrameworkCore; // ExecuteUpdate, ExecuteDelete +using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry using Microsoft.EntityFrameworkCore.Storage; // IDbContextTransaction -using Packt.Shared; +using Packt.Shared; // Northwind, Product partial class Program { - static void ListProducts(int? productIdToHighlight = null) + static void ListProducts(int[]? productIdsToHighlight = null) { using (Northwind db = new()) { @@ -21,7 +22,8 @@ partial class Program { ConsoleColor previousColor = ForegroundColor; - if (productIdToHighlight == p.ProductId) + if ((productIdsToHighlight is not null) && + productIdsToHighlight.Contains(p.ProductId)) { ForegroundColor = ConsoleColor.Green; } @@ -39,6 +41,8 @@ partial class Program { using (Northwind db = new()) { + if (db.Products is null) return (0, 0); + Product p = new() { CategoryId = categoryId, @@ -64,7 +68,9 @@ partial class Program { using (Northwind db = new()) { - // get first product whose name starts with name + if (db.Products is null) return (0, 0); + + // Get the first product whose name starts with name. Product updateProduct = db.Products.First( p => p.ProductName.StartsWith(productNameStartsWith)); @@ -86,15 +92,16 @@ partial class Program arg0: t.GetDbTransaction().IsolationLevel); IQueryable? products = db.Products?.Where( - p => p.ProductName.StartsWith(productNameStartsWith)); + p => p.ProductName.StartsWith(productNameStartsWith)); - if (products is null) + if ((products is null) || (!products.Any())) { WriteLine("No products found to delete."); return 0; } else { + if (db.Products is null) return 0; db.Products.RemoveRange(products); } @@ -104,4 +111,47 @@ partial class Program } } } + + static (int affected, int[]? productIds) IncreaseProductPricesBetter( + string productNameStartsWith, decimal amount) + { + using (Northwind db = new()) + { + if (db.Products is null) return (0, null); + + // Get products whose name starts with name. + IQueryable? products = db.Products.Where( + p => p.ProductName.StartsWith(productNameStartsWith)); + + int affected = products.ExecuteUpdate(s => s.SetProperty( + p => p.Cost, // Property selector lambda expression. + p => p.Cost + amount)); // Value to update to lambda expression. + + int[] productIds = products.Select(p => p.ProductId).ToArray(); + + return (affected, productIds); + } + } + + static int DeleteProductsBetter(string productNameStartsWith) + { + using (Northwind db = new()) + { + int affected = 0; + + IQueryable? products = db.Products?.Where( + p => p.ProductName.StartsWith(productNameStartsWith)); + + if ((products is null) || (!products.Any())) + { + WriteLine("No products found to delete."); + return 0; + } + else + { + affected = products.ExecuteDelete(); + } + return affected; + } + } } \ No newline at end of file diff --git a/vscode/Chapter10/WorkingWithEFCore/Program.cs b/vscode/Chapter10/WorkingWithEFCore/Program.cs index 650cf0e..6e6d82d 100644 --- a/vscode/Chapter10/WorkingWithEFCore/Program.cs +++ b/vscode/Chapter10/WorkingWithEFCore/Program.cs @@ -1,42 +1,59 @@ using Packt.Shared; -/* + Northwind db = new(); WriteLine($"Provider: {db.Database.ProviderName}"); -*/ + // QueryingCategories(); -//FilteredIncludes(); +// FilteredIncludes(); -//QueryingProducts(); +// QueryingProducts(); //QueryingWithLike(); //GetRandomProduct(); +/* var resultAdd = AddProduct(categoryId: 6, productName: "Bob's Burgers", price: 500M); if (resultAdd.affected == 1) { - WriteLine("Add product successful."); + WriteLine($"Add product successful with ID: {resultAdd.productId}."); } -ListProducts(productIdToHighlight: resultAdd.productId); +ListProducts(productIdsToHighlight: new[] { resultAdd.productId }); +*/ +/* var resultUpdate = IncreaseProductPrice( productNameStartsWith: "Bob", amount: 20M); if (resultUpdate.affected == 1) +{ + WriteLine($"Increase price success for ID: {resultUpdate.productId}."); +} + +ListProducts(productIdsToHighlight: new[] { resultUpdate.productId }); +*/ + +/* +var resultUpdateBetter = IncreaseProductPricesBetter( + productNameStartsWith: "Bob", amount: 20M); + +if (resultUpdateBetter.affected > 0) { WriteLine("Increase product price successful."); } -ListProducts(productIdToHighlight: resultUpdate.productId); +ListProducts(productIdsToHighlight: resultUpdateBetter.productIds); +*/ +/* WriteLine("About to delete all products whose name starts with Bob."); -Write("Press Enter to continue: "); +Write("Press Enter to continue or any other key to exit: "); if (ReadKey(intercept: true).Key == ConsoleKey.Enter) { int deleted = DeleteProducts(productNameStartsWith: "Bob"); @@ -45,4 +62,19 @@ if (ReadKey(intercept: true).Key == ConsoleKey.Enter) else { WriteLine("Delete was canceled."); -} \ No newline at end of file +} +*/ + +/* +WriteLine("About to delete all products whose name starts with Bob."); +Write("Press Enter to continue or any other key to exit: "); +if (ReadKey(intercept: true).Key == ConsoleKey.Enter) +{ + int deleted = DeleteProductsBetter(productNameStartsWith: "Bob"); + WriteLine($"{deleted} product(s) were deleted."); +} +else +{ + WriteLine("Delete was canceled."); +} +*/ \ No newline at end of file diff --git a/vscode/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj b/vscode/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj index a90f1ab..a594e51 100644 --- a/vscode/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj +++ b/vscode/Chapter10/WorkingWithEFCore/WorkingWithEFCore.csproj @@ -7,20 +7,20 @@ enable - - - + + + - - - - - + + + + + - - - Always - - + + + PreserveNewest + +