mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-01-06 16:20:03 +01:00
Initial commit
This commit is contained in:
parent
49387f9c3f
commit
21a652df69
|
|
@ -1,10 +1,11 @@
|
|||
using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry<T>
|
||||
using Microsoft.EntityFrameworkCore; // ExecuteUpdate, ExecuteDelete
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry<T>
|
||||
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<Product>? 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<Product>? 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<Product>? 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
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.");
|
||||
}
|
||||
*/
|
||||
|
|
@ -7,20 +7,20 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-*" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Northwind.db">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="Northwind.db">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry<T>
|
||||
using Microsoft.EntityFrameworkCore; // ExecuteUpdate, ExecuteDelete
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking; // EntityEntry<T>
|
||||
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<Product>? 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<Product>? 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<Product>? 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
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.");
|
||||
}
|
||||
*/
|
||||
|
|
@ -7,20 +7,20 @@
|
|||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-*" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0-*" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="7.0.0-*" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Northwind.db">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="Northwind.db">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
Loading…
Reference in a new issue