“构建失败"关于数据库优先脚手架-DbContext [英] "Build failed" on Database First Scaffold-DbContext
问题描述
我正在尝试从数据库生成类(EntityFramework 的数据库优先方法).
为方便起见,我或多或少地跟着本教程走:https://docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html
我现在正在 Visual Studio 包管理器控制台中运行这行代码的等价物:
Scaffold-DbContext "Server=(localdb)mssqllocaldb;Database=Blogging;Trusted_Connection=True;"Microsoft.EntityFrameworkCore.SqlServer -Verbose
这行代码产生了错误(开启了 -Verbose 模式):
使用启动项目'EFSandbox'.使用项目EntityFrameworkCore"构建开始...构建失败.
我看不到其他选项可以产生任何有意义的输出,也没有看到有关此特定错误的文档.如果有帮助,这个项目目前没有 project.json 文件.一切都在 .csproj 文件中,我没有手动编辑过.
两个最重要的提示:
[1] - 在运行新的脚手架命令之前,确保您的项目完全构建.
否则...
- 您将开始编写一行代码.
- 您会发现模型中缺少必需的数据库列.
- 你会去尝试搭建它.
- 20 分钟后,您将意识到构建(和脚手架命令)失败的原因是因为您实际上编写了一半的代码行.糟糕!
[2] - 签入源代码管理或制作副本:
- 允许您轻松验证更改的内容.
- 允许在需要时回滚.
如果您不走运或犯错,您可能会遇到一些非常烦人的鸡和蛋"问题.
其他问题:
如果您有多个 DLL,请确保您没有生成错误的项目.出现构建失败"消息的原因有很多,但最愚蠢的情况是,如果您要搭建的项目中没有安装 EFCore.
在包管理器控制台中有一个 Default project
下拉菜单,如果您错过了预期的更改,这可能就是您的新文件结束的地方.
比记住设置下拉菜单更好的解决方案是将 -Project
开关添加到您的脚手架命令中.
这是我使用的完整命令:
对于 EF Core 2
<块引用>Scaffold-DbContext -Connection"服务器=(本地);数据库=DefenderRRCart;集成安全=真;Trusted_Connection=真;"-提供者Microsoft.EntityFrameworkCore.SqlServer -OutputDir RRStoreContext.Models-context RRStoreContext -Project RR.DataAccess -force
对于 EF Core 3
<块引用>dotnet ef dbcontext 脚手架"Server=tcp:XXXXX.database.windows.net,1433;初始Catalog=DATABASE_NAME;Persist Security Info=False;UserID=USERNAME;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection超时=30;"Microsoft.EntityFrameworkCore.SqlServer -o DB.Models--context-dir DB.Contexts --context RRDBContext --project RR.EF.csproj --force --use-database-names
注意:-force 将覆盖文件,但不会删除不再存在的文件.如果您从数据库中删除表,您必须自己删除旧的实体文件(只需在资源管理器中按日期排序并删除旧的).
完整的脚手架参考:
EF 核心 2:
https://docs.efproject.net/en/latest/miscellaneous/cli/powershell.html#scaffold-dbcontext(这个
EF 核心 3:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
I'm trying to generate classes from a database (EntityFramework's database first approach).
For convenience, I'm more or less walking along with this tutorial: https://docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html
I'm at a point where I am running the equivalent of this line of code in the Visual Studio Package Manager Console:
Scaffold-DbContext "Server=(localdb)mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Verbose
This line of code is generating the error (with -Verbose mode on):
Using startup project 'EFSandbox'.
Using project 'EntityFrameworkCore'
Build started...
Build failed.
I see no other options that produce any meaningful output, and I see no documentation on this particular error. If it helps at all, this project does not have a project.json file, currently. Everything is in the .csproj file, which I have not manually edited.
Two most important tips:
[1] - Make sure that your project builds completely before you run a new scaffold command.
Otherwise...
- You'll start writing a line of code.
- You'll realize a required DB column is missing from your model.
- You'll go to try to scaffold it.
- Twenty minutes later you'll realize the reason your build (and scaffold command) is failing is because you literally have a half written line of code. Oops!
[2] - Check into source control or make a copy:
- Allows you to easily verify what changed.
- Allows rollback if needed.
You can get some very annoying 'chicken and egg' problems if you get unlucky or make a mistake.
Other problems:
If you have multiple DLLs make sure you aren't generating into the wrong project. A 'Build failed' message can occur for many reasons, but the dumbest would be if you don't have EFCore installed in the project you're scaffolding into.
In the package manager console there is a Default project
dropdown and that's probably where your new files ended up if you're missing an expected change.
A better solution than remembering to set a dropdown is to add the -Project
switch to your scaffolding command.
This is the full command I use:
For EF Core 2
Scaffold-DbContext -Connection "Server=(local);Database=DefenderRRCart;Integrated Security=True;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir RRStoreContext.Models -context RRStoreContext -Project RR.DataAccess -force
For EF Core 3
dotnet ef dbcontext scaffold "Server=tcp:XXXXX.database.windows.net,1433;Initial Catalog=DATABASE_NAME;Persist Security Info=False;User ID=USERNAME;Password=PASSWORD;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" Microsoft.EntityFrameworkCore.SqlServer -o DB.Models --context-dir DB.Contexts --context RRDBContext --project RR.EF.csproj --force --use-database-names
Note: -force will overwrite files but not remove ones that don't exist any more. If you delete tables from your DB you must delete the old entity files yourself (just sort in Explorer by date and delete the old ones).
Full Scaffolding reference:
EF Core 2:
https://docs.efproject.net/en/latest/miscellaneous/cli/powershell.html#scaffold-dbcontext (this
EF Core 3:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
这篇关于“构建失败"关于数据库优先脚手架-DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!