Entity Framework 核心:将数据库脚手架自动化到 class 库中
Entity Framework Core: Automated DB scaffolding into a class library
在 this tutorial 之后,可以通过命令行使用 Entity Framework Core 构建数据库上下文。
脚手架有一些奇怪的要求
- 它需要一个入口点 aka main 方法(没有进入 class 库的脚手架)
- 完整的项目必须是可编译的(在搭建脚手架之前)
在我当前的项目中,我需要 "shared project" 中的模型 classes(不是直接 class 库)。共享项目没有入口点。
(我目前有很多脚手架,因为数据库工程师在数据库优先方法中更新了很多数据库模型)。
为了创建一些自动化的脚手架任务脚本,我计划自动执行以下任务:
- 创建一个新的、空的和临时的 dot net 命令行应用程序(它是可构建的并且有一个入口点)
- 添加所需的包
- 搭建数据库上下文和模型 classes
- 将生成的 classes 移动到 library/shared 项目
- 删除临时项目
到目前为止,我设法自动化了第一点。
但我想不出如何将 ItemGroup DotNetCliToolReference 添加到 .csproj 文件的 xml。
是否有任何 dotnet cli 命令可以让我添加 DotNetCliToolReference 而不仅仅是包和项目到项目的引用?
请问还有其他提示吗?
您试过我的 "EF Core Power Tools" - 他们也许可以帮助您自动化流程吗?
- 添加另一个项目作为当前使用的参考
dotnet add reference
:
dotnet add reference lib1/lib1.csproj
- 对于包使用
dotnet add package
:
dotnet add package Newtonsoft.Json
从 EF Core 2.1 开始,引用包含在 .NET Core SDK 中,这意味着您不必将引用添加到临时项目。
https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-entity-framework-core-2-1/
The dotnet-ef commands now ship as part of the .NET Core SDK, so it will no longer be necessary to use DotNetCliToolReference in the project to be able to use migrations or to scaffold a DbContext from an existing database.
我创建了一个快速的 PowerShell 脚本,该脚本被添加到类库中,然后创建一个临时项目并构建 dbcontext 并在最后清理所有内容。
param(
[Parameter(Mandatory=$true)][string]$username,
[Parameter(Mandatory=$true)][string]$password,
[Parameter(Mandatory=$true)][string]$datasource,
[Parameter(Mandatory=$true)][string]$catalog,
[Parameter(Mandatory=$true)][string]$contextName
)
$workingDir = $PSScriptRoot;
Write-Host "Project dir= $workingDir";
Write-Host "Creating temp project to run scaffold on";
cd ..
mkdir temp
cd temp
dotnet new web
Write-Host "Done creating temp project";
Write-Host "Scaffolding dbcontext into project";
dotnet ef dbcontext scaffold "data source=$($datasource);initial catalog=$($catalog);user id=$($username);password=$($password);MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer --output-dir Entities --context $contextName --project temp.csproj
Write-Host "Done scaffolding dbcontext into temp project";
New-Item -ErrorAction Ignore -ItemType directory -Path "$workingDir/Entities";
Move-Item ./Entities/* "$workingDir/Entities" -force;
Write-Host "Scaffold completed! Starting clean-up";
cd ..
Remove-Item ./temp -force -Recurse;
cd $workingDir;
Write-Host "Clean-up completed!";
我确信脚本可以改进,但它现在的效果很好。
脚本的工作原理如下:
假设我们运行 classlib 目录中的脚本:c:/test/classlib
- 存放当前目录
- 创建临时 Web 项目:c:/test/temp
- 搭建 DBContext
- 将脚手架项目移动到类库(请注意脚本设置为覆盖当前实体目录)
- 删除临时项目
您仍然需要将名称空间更改为正确的名称空间。据我所知,对脚手架上命名空间的支持目前在 2.2 的列表中。希望对您有所帮助!
从 Nuget
安装 EntityFrameWorkCore.Sqlserver
和 EntityFrameWorkCore.Tools
在包管理器控制台中写入:
Scaffold-DbContext "Data Source=.;Initial Catalog="dbName";Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project "Project's class library Name"
在 this tutorial 之后,可以通过命令行使用 Entity Framework Core 构建数据库上下文。
脚手架有一些奇怪的要求
- 它需要一个入口点 aka main 方法(没有进入 class 库的脚手架)
- 完整的项目必须是可编译的(在搭建脚手架之前)
在我当前的项目中,我需要 "shared project" 中的模型 classes(不是直接 class 库)。共享项目没有入口点。
(我目前有很多脚手架,因为数据库工程师在数据库优先方法中更新了很多数据库模型)。
为了创建一些自动化的脚手架任务脚本,我计划自动执行以下任务:
- 创建一个新的、空的和临时的 dot net 命令行应用程序(它是可构建的并且有一个入口点)
- 添加所需的包
- 搭建数据库上下文和模型 classes
- 将生成的 classes 移动到 library/shared 项目
- 删除临时项目
到目前为止,我设法自动化了第一点。
但我想不出如何将 ItemGroup DotNetCliToolReference 添加到 .csproj 文件的 xml。
是否有任何 dotnet cli 命令可以让我添加 DotNetCliToolReference 而不仅仅是包和项目到项目的引用?
请问还有其他提示吗?
您试过我的 "EF Core Power Tools" - 他们也许可以帮助您自动化流程吗?
- 添加另一个项目作为当前使用的参考
dotnet add reference
:
dotnet add reference lib1/lib1.csproj
- 对于包使用
dotnet add package
:
dotnet add package Newtonsoft.Json
从 EF Core 2.1 开始,引用包含在 .NET Core SDK 中,这意味着您不必将引用添加到临时项目。
https://blogs.msdn.microsoft.com/dotnet/2018/05/30/announcing-entity-framework-core-2-1/
The dotnet-ef commands now ship as part of the .NET Core SDK, so it will no longer be necessary to use DotNetCliToolReference in the project to be able to use migrations or to scaffold a DbContext from an existing database.
我创建了一个快速的 PowerShell 脚本,该脚本被添加到类库中,然后创建一个临时项目并构建 dbcontext 并在最后清理所有内容。
param(
[Parameter(Mandatory=$true)][string]$username,
[Parameter(Mandatory=$true)][string]$password,
[Parameter(Mandatory=$true)][string]$datasource,
[Parameter(Mandatory=$true)][string]$catalog,
[Parameter(Mandatory=$true)][string]$contextName
)
$workingDir = $PSScriptRoot;
Write-Host "Project dir= $workingDir";
Write-Host "Creating temp project to run scaffold on";
cd ..
mkdir temp
cd temp
dotnet new web
Write-Host "Done creating temp project";
Write-Host "Scaffolding dbcontext into project";
dotnet ef dbcontext scaffold "data source=$($datasource);initial catalog=$($catalog);user id=$($username);password=$($password);MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer --output-dir Entities --context $contextName --project temp.csproj
Write-Host "Done scaffolding dbcontext into temp project";
New-Item -ErrorAction Ignore -ItemType directory -Path "$workingDir/Entities";
Move-Item ./Entities/* "$workingDir/Entities" -force;
Write-Host "Scaffold completed! Starting clean-up";
cd ..
Remove-Item ./temp -force -Recurse;
cd $workingDir;
Write-Host "Clean-up completed!";
我确信脚本可以改进,但它现在的效果很好。
脚本的工作原理如下:
假设我们运行 classlib 目录中的脚本:c:/test/classlib
- 存放当前目录
- 创建临时 Web 项目:c:/test/temp
- 搭建 DBContext
- 将脚手架项目移动到类库(请注意脚本设置为覆盖当前实体目录)
- 删除临时项目
您仍然需要将名称空间更改为正确的名称空间。据我所知,对脚手架上命名空间的支持目前在 2.2 的列表中。希望对您有所帮助!
从 Nuget
安装 在包管理器控制台中写入:
Scaffold-DbContext "Data Source=.;Initial Catalog="dbName";Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Project "Project's class library Name"
EntityFrameWorkCore.Sqlserver
和 EntityFrameWorkCore.Tools