Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tabs in scaffolding page #2377

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions entity-framework/core/managing-schemas/migrations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Three files are added to your project under the **Migrations** directory:
The timestamp in the filename helps keep them ordered chronologically so you can see the progression of changes.

### Namespaces

You are free to move Migrations files and change their namespace manually. New migrations are created as siblings of the last migration.

Alternatively you can use `-Namespace` (Package Manager Console) or `--namespace` (.NET Core CLI) to specify the namespace at generation time.
Expand All @@ -72,6 +73,8 @@ dotnet ef migrations add InitialCreate --namespace Your.Namespace
Add-Migration InitialCreate -Namespace Your.Namespace
```

***

## Update the database

Next, apply the migration to the database to create the schema.
Expand Down
62 changes: 42 additions & 20 deletions entity-framework/core/managing-schemas/scaffolding.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ The first argument to the command is a connection string to the database. The to

How you quote and escape the connection string depends on which shell you are using to execute the command. Refer to your shell's documentation for specifics. For example, PowerShell requires you to escape the `$` character, but not `\`.

``` powershell
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer
```
### [.NET Core CLI](#tab/dotnet-core-cli)

```dotnetcli
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" Microsoft.EntityFrameworkCore.SqlServer
```

### [Visual Studio](#tab/vs)

``` powershell
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer
```

***

### Configuration and User Secrets

If you have an ASP.NET Core project, you can use the `Name=<connection-string>` syntax to read the connection string from configuration.
Expand All @@ -49,22 +55,28 @@ The second argument is the provider name. The provider name is typically the sam

All tables in the database schema are reverse engineered into entity types by default. You can limit which tables are reverse engineered by specifying schemas and tables.

The `-Schemas` parameter in PMC and the `--schema` option in the CLI can be used to include every table within a schema.
### [.NET Core CLI](#tab/dotnet-core-cli)

`-Tables` (PMC) and `--table` (CLI) can be used to include specific tables.
The `--schema` option can be used to include every table within a schema, while `--table` can be used to include specific tables.

To include multiple tables in PMC, use an array.
To include multiple tables, specify the option multiple times:

``` powershell
Scaffold-DbContext ... -Tables Artist, Album
```dotnetcli
dotnet ef dbcontext scaffold ... --table Artist --table Album
```

To include multiple tables in the CLI, specify the option multiple times.
### [Visual Studio](#tab/vs)

```dotnetcli
dotnet ef dbcontext scaffold ... --table Artist --table Album
The `-Schemas` option can be used to include every table within a schema, while `-Tables` can be used to include specific tables.

To include multiple tables, use an array:

``` powershell
Scaffold-DbContext ... -Tables Artist, Album
```

***

## Preserving names

Table and column names are fixed up to better match the .NET naming conventions for types and properties by default. Specifying the `-UseDatabaseNames` switch in PMC or the `--use-database-names` option in the CLI will disable this behavior preserving the original database names as much as possible. Invalid .NET identifiers will still be fixed and synthesized names like navigation properties will still conform to .NET naming conventions.
Expand Down Expand Up @@ -95,28 +107,38 @@ The scaffolded DbContext class name will be the name of the database suffixed wi

## Directories and namespaces

The entity classes and a DbContext class are scaffolded into the project's root directory and use the project's default namespace. You can specify the directory where classes are scaffolded using `-OutputDir` (PMC) or `--output-dir` (CLI).
The entity classes and a DbContext class are scaffolded into the project's root directory and use the project's default namespace.

You can also use `-ContextDir` (PMC) and `--context-dir` (CLI) to scaffold the DbContext class into a separate directory from the entity type classes.
### [.NET Core CLI](#tab/dotnet-core-cli)

``` powershell
Scaffold-DbContext ... -ContextDir Data -OutputDir Models
```
You can specify the directory where classes are scaffolded using `--output-dir`, and `--context-dir` can be used to scaffold the DbContext class into a separate directory from the entity type classes:

```dotnetcli
dotnet ef dbcontext scaffold ... --context-dir Data --output-dir Models
```

By default, the namespace will be the root namespace plus the names of any subdirectories under the project's root directory. However, from EFCore 5.0 onwards, you can override the namespace for all output classes by using `-Namespace` (PMC) or `--namespace` (CLI). You can also override the namespace for just the DbContext class using `-ContextNamespace` (PMC) or `--context-namespace` (CLI).
By default, the namespace will be the root namespace plus the names of any subdirectories under the project's root directory. However, from EFCore 5.0 onwards, you can override the namespace for all output classes by using `--namespace`. You can also override the namespace for just the DbContext class using `--context-namespace`:

```dotnetcli
dotnet ef dbcontext scaffold ... --namespace Your.Namespace --context-namespace Your.DbContext.Namespace
```

### [Visual Studio](#tab/vs)

You can specify the directory where classes are scaffolded using `-OutputDir`, and `-ContextDir` can be used to scaffold the DbContext class into a separate directory from the entity type classes:

``` powershell
Scaffold-DbContext ... -Namespace Your.Namespace -ContextNamespace Your.DbContext.Namespace
Scaffold-DbContext ... -ContextDir Data -OutputDir Models
```

```dotnetcli
dotnet ef dbcontext scaffold ... --namespace Your.Namespace --context-namespace Your.DbContext.Namespace
By default, the namespace will be the root namespace plus the names of any subdirectories under the project's root directory. However, from EFCore 5.0 onwards, you can override the namespace for all output classes by using `-Namespace`. You can also override the namespace for just the DbContext class using `-ContextNamespace`.

``` powershell
Scaffold-DbContext ... -Namespace Your.Namespace -ContextNamespace Your.DbContext.Namespace
```

***

## How it works

Reverse engineering starts by reading the database schema. It reads information about tables, columns, constraints, and indexes.
Expand Down
4 changes: 2 additions & 2 deletions entity-framework/core/miscellaneous/cli/powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ To make sure that you're getting the latest version of the tools, we recommend t
```xml
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
</ItemGroup>
```

Expand Down