mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
Initial commit
This commit is contained in:
parent
81c5936bb9
commit
01d6ccf414
|
|
@ -601,6 +601,7 @@
|
|||
|
||||
## EF Core querying and manipulating
|
||||
- Filtered include: https://docs.microsoft.com/en-us/ef/core/querying/related-data/eager#filtered-include
|
||||
- Simple logging: https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/simple-logging
|
||||
- Query tags: https://docs.microsoft.com/en-us/ef/core/querying/tags
|
||||
- Loading patterns: https://docs.microsoft.com/en-us/ef/core/querying/related-data
|
||||
- Pooling database contexts: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#dbcontext-pooling
|
||||
|
|
|
|||
|
|
@ -76,3 +76,88 @@ We did not have to use SQL Server Management Studio to execute the database scri
|
|||
6. Right-click the **Products** table, choose **Show Table Data**, and note the 77 rows of products are returned.
|
||||
7. To see the details of the **Products** table columns and types, right-click **Products** and choose **Open Table Definition**, or double-click the table in **Server Explorer**.
|
||||
|
||||
## Connecting to a database
|
||||
|
||||
To connect to an SQL Server database, we need to know multiple pieces of information, as shown in the following list:
|
||||
- The name of the server (and the instance if it has one).
|
||||
- The name of the database.
|
||||
- Security information, such as username and password, or if we should pass the currently logged-on user's credentials automatically.
|
||||
|
||||
We specify this information in a **connection string**.
|
||||
|
||||
For backward compatibility, there are multiple possible keywords we can use in an SQL Server connection string for the various parameters, as shown in the following list:
|
||||
- `Data Source` or `server` or `addr`: These keywords are the name of the server (and an optional instance). You can use a dot `.` to mean the local server.
|
||||
- `Initial Catalog` or `database`: These keywords are the name of the database.
|
||||
- `Integrated Security` or `trusted_connection`: These keywords are set to `true` or `SSPI` to pass the thread's current Windows user credentials.
|
||||
- `MultipleActiveResultSets`: This keyword is set to `true` to enable a single connection to be used to work with multiple tables simultaneously to improve efficiency. It is used for lazy loading rows from related tables.
|
||||
|
||||
As described in the list above, when you write code to connect to an SQL Server database, you need to know its server name. The server name depends on the edition and version of SQL Server that you will connect to, as shown in the following table:
|
||||
|
||||
|
||||
| SQL Server edition | Server name \ Instance name |
|
||||
| --- | --- |
|
||||
| LocalDB 2012 | `(localdb)\v11.0` |
|
||||
| LocalDB 2016 or later | `(localdb)\mssqllocaldb` |
|
||||
| Express | `.\sqlexpress` |
|
||||
| Full/Developer (default instance) | `.` |
|
||||
| Full/Developer (named instance) | `.\csdotnetbook` |
|
||||
|
||||
> **Good Practice**: Use a dot `.` as shorthand for the local computer name. Remember that server names for SQL Server are made of two parts: the name of the computer and the name of an SQL Server instance. You provide instance names during custom installation.
|
||||
|
||||
## Defining the Northwind database context class
|
||||
|
||||
1. In the `WorkingWithEFCore` project, add a package reference to the EF Core data provider for SQL Server and globally and statically import the `System.Console` class for all C# files, as shown in the following markup:
|
||||
```xml
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference
|
||||
Include="Microsoft.EntityFrameworkCore.SqlServer"
|
||||
Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
```
|
||||
2. Build the `WorkingWithEFCore` project to restore packages.
|
||||
3. Add a new class file named `Northwind.cs`.
|
||||
4. In `Northwind.cs`, define a class named `Northwind`, import the main namespace for EF Core, make the class inherit from `DbContext`, and in an `OnConfiguring` method, configure the options builder to use SQL Server, as shown in the following code:
|
||||
```cs
|
||||
using Microsoft.EntityFrameworkCore; // DbContext, DbContextOptionsBuilder
|
||||
|
||||
namespace Packt.Shared;
|
||||
|
||||
// this manages the connection to the database
|
||||
public class Northwind : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(
|
||||
DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
string connection = "Data Source=.;" +
|
||||
"Initial Catalog=Northwind;" +
|
||||
"Integrated Security=true;" +
|
||||
"MultipleActiveResultSets=true;";
|
||||
|
||||
optionsBuilder.UseSqlServer(connection);
|
||||
}
|
||||
}
|
||||
```
|
||||
5. In `Program.cs`, delete the existing statements and then import the `Packt.Shared` namespace and output the database provider, as shown in the following code:
|
||||
```cs
|
||||
using Packt.Shared;
|
||||
|
||||
Northwind db = new();
|
||||
WriteLine($"Provider: {db.Database.ProviderName}");
|
||||
```
|
||||
6. Run the console app and note the output showing the database connection string and which database provider you are using, as shown in the following output:
|
||||
```
|
||||
Connection string: Data Source=.;Initial Catalog=Northwind;Integrated Security=true;MultipleActiveResultSets=true;
|
||||
Provider: Microsoft.EntityFrameworkCore.SqlServer
|
||||
```
|
||||
|
||||
## Scaffolding models using an existing database
|
||||
|
||||
For SQL Server, change the database provider and connection string, as shown in the following command:
|
||||
```
|
||||
dotnet ef dbcontext scaffold "Data Source=.;Initial Catalog=Northwind;Integrated Security=true;" Microsoft.EntityFrameworkCore.SqlServer --table Categories --table Products --output-dir AutoGenModels --namespace WorkingWithEFCore.AutoGen --data-annotations --context Northwind
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue