-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathChapter10.dib
159 lines (107 loc) · 3.57 KB
/
Chapter10.dib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!markdown
# Chapter 10 - Working with Data Using Entity Framework Core
- Understanding modern databases
- Setting up EF Core
- Defining EF Core models
- Querying EF Core models
- Loading patterns with EF Core
- Manipulating data with EF Core
- Working with transactions
- Code First EF Core models
#!markdown
# Setting up EF Core
[.NET Interactive with SQL](https://devblogs.microsoft.com/dotnet/net-interactive-with-sql-net-notebooks-in-visual-studio-code/)
#!csharp
#r "nuget:Microsoft.DotNet.Interactive.SqlServer,1.0.0-beta.21553.5"
#!sql
#!connect mssql -h
#!sql
#!connect mssql --kernel-name Northwind "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;MultipleActiveResultSets=true;"
#!sql
#!sql-Northwind
SELECT * FROM Categories
#!sql
#!connect mssql --create-dbcontext --kernel-name Northwind "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;MultipleActiveResultSets=true;"
#!markdown
## Choosing an EF Core database provider
Add package references to the EF Core data provider for both SQL Server and SQLite.
#!csharp
#r "nuget:Microsoft.EntityFrameworkCore.Sqlite,5.0.11"
#r "nuget:Microsoft.EntityFrameworkCore.SqlServer,5.0.11"
#!markdown
Define a constant to switch between providers.
#!csharp
public class ProjectConstants
{
public const string DatabaseProvider = "SQLite"; // or "SQLServer"
}
#!csharp
using static System.Console;
#!csharp
WriteLine($"Using {ProjectConstants.DatabaseProvider} database provider.");
#!markdown
## Defining the Northwind database context class
#!csharp
using System.IO;
using Microsoft.EntityFrameworkCore; // DbContext, DbContextOptionsBuilder
#!csharp
public class Northwind : DbContext
{
protected override void OnConfiguring(
DbContextOptionsBuilder optionsBuilder)
{
if (ProjectConstants.DatabaseProvider == "SQLite")
{
string path = Path.Combine(
Environment.CurrentDirectory, "Northwind.db");
WriteLine($"Using {path} database file.");
optionsBuilder.UseSqlite($"Filename={path}");
}
else
{
string connection = "Data Source=.;" +
"Initial Catalog=Northwind;" +
"Integrated Security=true;" +
"MultipleActiveResultSets=true;";
optionsBuilder.UseSqlServer(connection);
}
}
}
#!markdown
## Defining the Category and Product entity classes
#!csharp
#nullable enable
using System.ComponentModel.DataAnnotations; // [Required], [StringLength]
using System.ComponentModel.DataAnnotations.Schema; // [Column]
public class Category
{
// these properties map to columns in the database
public int CategoryId { get; set; }
public string? CategoryName { get; set; }
[Column(TypeName = "ntext")]
public string? Description { get; set; }
// defines a navigation property for related rows
public virtual ICollection<Product> Products { get; set; }
public Category()
{
// to enable developers to add products to a Category we must
// initialize the navigation property to an empty collection
this.Products = new HashSet<Product>();
}
}
public class Product
{
public int ProductId { get; set; } // primary key
[Required]
[StringLength(40)]
public string ProductName { get; set; } = null!;
[Column("UnitPrice", TypeName = "money")]
public decimal? Cost { get; set; } // property name != column name
[Column("UnitsInStock")]
public short? Stock { get; set; }
public bool Discontinued { get; set; }
// these two define the foreign key relationship
// to the Categories table
public int CategoryId { get; set; }
public virtual Category Category { get; set; } = null!;
}