Scaffold-DbContext 报错解决方案

作者:outlela  来源:本站原创   发布时间:2023-3-9 9:27:47

1、Scaffold-DbContext : 无法将“Scaffold-DbContext”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

    只需要引入包名:Microsoft.EntityFrameworkCore.Tools

2、A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - 证书链是由不受信任的颁发机构颁发的。),译文:已成功与服务器建立连接,但在登录过程中发生错误。(提供程序: SSL 提供程序,错误: 0- 证书链是由不受信任的颁发机构颁发的。)

    只需要在数据库链接中添加:Encrypt=False

3、使用Scaffold-DbContext 一般需要引用以下包:

    1、Microsoft.EntityFrameworkCore

    2、Microsoft.EntityFrameworkCore.Design

    3、Microsoft.EntityFrameworkCore.SqlServer(对应具体数据库,这里是SqlServer

    4、Microsoft.EntityFrameworkCore.Tools

4、To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. (若要保护连接字符串中的潜在敏感信息,应将其移出源代码。通过使用 Name= 语法从配置中读取连接字符串,可以避免基架连接字符串 - 请参阅 https://go.microsoft.com/fwlink/?linkid=2131148。有关存储连接字符串的更多指南,请参阅 http://go.microsoft.com/fwlink/?LinkId=723263。)

    如果是通过以下语句生成的Entity,会把数据库链接保存在Context文件里面,所以会提示不安全,应该从源代码中移除。

Scaffold-DbContext "Data Source=服务器;Initial Catalog=数据库名;User ID=登录账号;Password=密码." Microsoft.EntityFrameworkCore.SqlServer -OutputDir 文件夹 -Force

    可以改为在配置文件 appsettings.json 中配置数据库连接字符串,如下:

  "ConnectionStrings": {
      "自定义名称": "Data Source=xxxx;Initial Catalog=数据库铝盘;User ID=登录名;Password=密码"
  }

    改好以后:Scaffold-DbContext 语句也得改一下:

Scaffold-DbContext Name=自定义的名称 Microsoft.EntityFrameworkCore.SqlServer -Project 生成在哪个项目,填写命名空间 -OutputDir 生成在哪个文件夹 -ContextDir Context生成在哪个文件夹 -ContextNamespace Context的命名空间 -Namespace Entity的命名空间 -Context 指定Context的名称 -Force

    目前Scaffold-DbContext 只遇到了以上问题,记录一下。





*本文最后修改于:2023-3-9 10:2:24
本文标签: Scaffold-DbContext EntityFrameworkCore EntityFramework .NET C#
本文由本站原创发布, 本文链接地址:https://outlela.com/share/167.html
转载或引用请保留地址并注明出处:outlela.com