diff --git a/entity-framework/core/managing-schemas/migrations/index.md b/entity-framework/core/managing-schemas/migrations/index.md index cda9e519c1..8ee0360ab3 100644 --- a/entity-framework/core/managing-schemas/migrations/index.md +++ b/entity-framework/core/managing-schemas/migrations/index.md @@ -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. @@ -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. diff --git a/entity-framework/core/managing-schemas/scaffolding.md b/entity-framework/core/managing-schemas/scaffolding.md index f7e972d1f9..4bfad3095b 100644 --- a/entity-framework/core/managing-schemas/scaffolding.md +++ b/entity-framework/core/managing-schemas/scaffolding.md @@ -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=` syntax to read the connection string from configuration. @@ -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. @@ -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. diff --git a/entity-framework/core/miscellaneous/cli/powershell.md b/entity-framework/core/miscellaneous/cli/powershell.md index fa24738630..f68ac21df4 100644 --- a/entity-framework/core/miscellaneous/cli/powershell.md +++ b/entity-framework/core/miscellaneous/cli/powershell.md @@ -31,8 +31,8 @@ To make sure that you're getting the latest version of the tools, we recommend t ```xml - - + + ```