“构建失败”;在数据库第一个脚手架上-DbContext [英] "Build failed" on Database First Scaffold-DbContext

查看:146
本文介绍了“构建失败”;在数据库第一个脚手架上-DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库生成类(EntityFramework的数据库优先方法)。

I'm trying to generate classes from a database (EntityFramework's database first approach).

为了方便起见,我或多或少地与本教程一起学习:
https://docs.efproject.net /en/latest/platforms/full-dotnet/existing-db.html

For convenience, I'm more or less walking along with this tutorial: https://docs.efproject.net/en/latest/platforms/full-dotnet/existing-db.html

我正处在运行这一行的位置Visual Studio程序包管理器控制台中的代码集:

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

这行代码生成错误(在-Verbose模式下):

This line of code is generating the error (with -Verbose mode on):

Using startup project 'EFSandbox'.
Using project 'EntityFrameworkCore'
Build started...
Build failed.

我看不到其他产生任何有意义输出的选项,也看不到有关此特定错误的文档。如果有帮助,则该项目当前没有project.json文件。一切都在.csproj文件中,我没有手动对其进行编辑。

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.

推荐答案

两个最重要的提示:


[1]-运行新的脚手架命令之前,请确保您的项目已完全构建

否则...


  • 您将开始编写一行代码。

  • 您将意识到模型中缺少必需的数据库列。

  • 您将尝试对其进行脚手架。

  • 二十分钟后,您将意识到构建(和脚手架命令)失败的原因是因为您实际上只写了一半的代码。糟糕!

  • 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]-进入源代码管理或进行复制:

[2] - Check into source control or make a copy:


  • 使您可以轻松地验证更改。

  • 如果需要,可以进行回滚。

您可以获得一些

如果您有多个DLL,请确保您没有生成错误的项目。出于多种原因可能会出现 Build failed消息,但是最愚蠢的是如果您要脚手架的项目中未安装EFCore。

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.

更好解决方案比记得设置下拉菜单要好得多,那就是将 -Project 开关添加到脚手架命令中。

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:


Scaffold-DbContext -Connection
Server =(本地) ; Database = DefenderRRCart; Integrated
Security = True; Trusted_Connection = True; -提供者
Microsoft.EntityFrameworkCore.SqlServer -OutputDir RRStoreContext.Models
-上下文RRStoreContext-项目RR.DataAccess -force

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


EF Core 3



dotnet ef dbcontext支架
" Server = tcp:XXXXX.database.windows.net,1433;初始
目录= DATABASE_NAME;持久性安全信息= False;用户
ID =用户名;密码=密码; MultipleActiveResultSets = False;加密= True; TrustServerCertificate = False;连接
超时= 30;" Microsoft.EntityFrameworkCore.SqlServer -o DB.Models
--context-dir DB.Contexts --context RRDBContext --project RR.EF.csproj --force --use-database-names

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

注意:-force将覆盖文件,但不会删除不再存在的文件。如果要从数据库中删除表,则必须自己删除旧的实体文件(只需在Explorer中按日期排序并删除旧的实体文件即可。)。

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

https://docs.efproject.net/zh/latest/miscellaneous/cli/powershell.html#scaffold-dbcontext (此

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

这篇关于“构建失败”;在数据库第一个脚手架上-DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆