mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-05 06:15:24 +00:00
Initial commit
This commit is contained in:
parent
01d6ccf414
commit
dd097904c2
54 changed files with 37154 additions and 0 deletions
47
vscode/Chapter10/Ch10Ex02DataSerialization/Northwind.cs
Normal file
47
vscode/Chapter10/Ch10Ex02DataSerialization/Northwind.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
using Microsoft.EntityFrameworkCore; // DbContext, DbContextOptionsBuilder
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
|
||||
namespace Packt.Shared;
|
||||
|
||||
// this manages the connection to the database
|
||||
public class Northwind : DbContext
|
||||
{
|
||||
// these properties map to tables in the database
|
||||
public DbSet<Category>? Categories { get; set; }
|
||||
public DbSet<Product>? Products { get; set; }
|
||||
|
||||
protected override void OnConfiguring(
|
||||
DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
string path = Path.Combine(
|
||||
Environment.CurrentDirectory, "Northwind.db");
|
||||
|
||||
string connection = $"Filename={path}";
|
||||
|
||||
ConsoleColor previousColor = ForegroundColor;
|
||||
ForegroundColor = ConsoleColor.DarkYellow;
|
||||
WriteLine($"Connection: {connection}");
|
||||
ForegroundColor = previousColor;
|
||||
|
||||
optionsBuilder.UseSqlite(connection);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(
|
||||
ModelBuilder modelBuilder)
|
||||
{
|
||||
// example of using Fluent API instead of attributes
|
||||
// to limit the length of a category name to 15
|
||||
modelBuilder.Entity<Category>()
|
||||
.Property(category => category.CategoryName)
|
||||
.IsRequired() // NOT NULL
|
||||
.HasMaxLength(15);
|
||||
|
||||
if (Database.ProviderName?.Contains("Sqlite") ?? false)
|
||||
{
|
||||
// added to "fix" the lack of decimal support in SQLite
|
||||
modelBuilder.Entity<Product>()
|
||||
.Property(product => product.Cost)
|
||||
.HasConversion<double>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue