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

Specify how to use Namespace and ContextNamespace for migrations and scaffolding. #2333

Merged
merged 2 commits into from
Apr 28, 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
15 changes: 14 additions & 1 deletion entity-framework/core/managing-schemas/migrations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ 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.

> [!TIP]
> You are free to move Migrations files and change their namespace. New migrations are created as siblings of the last migration.
> 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.
lajones marked this conversation as resolved.
Show resolved Hide resolved
> ### [.NET Core CLI](#tab/dotnet-core-cli)
>
> ```dotnetcli
> dotnet ef migrations add InitialCreate --namespace Your.Namespace
> ```
>
> ### [Visual Studio](#tab/vs)
>
> ``` powershell
> Add-Migration InitialCreate -Namespace Your.Namespace
> ```

## Update the database

Expand Down
12 changes: 11 additions & 1 deletion entity-framework/core/managing-schemas/scaffolding.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ 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 namespace will be the root namespace plus the names of any subdirectories under the project's root directory.
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).

You can also use `-ContextDir` (PMC) and `--context-dir` (CLI) to scaffold the DbContext class into a separate directory from the entity type classes.

Expand All @@ -105,6 +105,16 @@ Scaffold-DbContext ... -ContextDir Data -OutputDir Models

```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 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).

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

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

## How it works
Expand Down
11 changes: 7 additions & 4 deletions entity-framework/core/miscellaneous/cli/dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,13 @@ Options:

| | Option | Description |
|:----------------|:-----------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <nobr>-d</nobr> | `--data-annotations` | Use attributes to configure the model (where possible). If this option is omitted, only the fluent API is used. |
| <nobr>`-d`</nobr> | `--data-annotations` | Use attributes to configure the model (where possible). If this option is omitted, only the fluent API is used. |
| `-c` | `--context <NAME>` | The name of the `DbContext` class to generate. |
| | `--context-dir <PATH>` | The directory to put the `DbContext` class file in. Paths are relative to the project directory. Namespaces are derived from the folder names. |
| | `--context-namespace <NAMESPACE>` | The namespace to use for the generated `DbContext` class. Note: overrides `--namespace`. |
| `-f` | `--force` | Overwrite existing files. |
| `-o` | `--output-dir <PATH>` | The directory to put entity class files in. Paths are relative to the project directory. |
| `-n` | `--namespace <NAMESPACE>` | The namespace to use for all generated classes. Defaults to generated from the root namespace and the output directory. |
| | <nobr>`--schema <SCHEMA_NAME>...`</nobr> | The schemas of tables to generate entity types for. To specify multiple schemas, repeat `--schema` for each one. If this option is omitted, all schemas are included. |
| `-t` | `--table <TABLE_NAME>`... | The tables to generate entity types for. To specify multiple tables, repeat `-t` or `--table` for each one. If this option is omitted, all tables are included. |
| | `--use-database-names` | Use table and column names exactly as they appear in the database. If this option is omitted, database names are changed to more closely conform to C# name style conventions. |
Expand All @@ -232,10 +234,10 @@ The following example scaffolds all schemas and tables and puts the new files in
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
```

The following example scaffolds only selected tables and creates the context in a separate folder with a specified name:
The following example scaffolds only selected tables and creates the context in a separate folder with a specified name and namespace:

```dotnetcli
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Blog -t Post --context-dir Context -c BlogContext
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Blog -t Post --context-dir Context -c BlogContext --context-namespace New.Namespace
```

## dotnet ef migrations add
Expand All @@ -252,7 +254,8 @@ Options:

| | Option | Description |
|:------------------|:-----------------------------------|:-----------------------------------------------------------------------------------------------------------------|
| <nobr>`-o`</nobr> | <nobr>`--output-dir <PATH>`</nobr> | The directory (and sub-namespace) to use. Paths are relative to the project directory. Defaults to "Migrations". |
| <nobr>`-o`</nobr> | <nobr>`--output-dir <PATH>`</nobr> | The directory use to output the files. Paths are relative to the target project directory. Defaults to "Migrations". |
| <nobr>`-n`</nobr> | <nobr>`--namespace <NAMESPACE>`</nobr> | The namespace to use for the generated classes. Defaults to generated from the output directory. |

## dotnet ef migrations list