Asp.net core ASP.NET核心:如何在Visual Studio代码中使用现有的SQL Server db&;CLI?

Asp.net core ASP.NET核心:如何在Visual Studio代码中使用现有的SQL Server db&;CLI?,asp.net-core,visual-studio-code,Asp.net Core,Visual Studio Code,我是asp.net核心的新手,随后我构建了一个新的mvc webapp,但它使用了一个新的SQLite数据库 我发现很多教程都使用SQLServer和VisualStudio。如何使用现有数据库在Visual Studio code中搭建脚手架?或者我怎样才能像教程一样做同样的事情 我现在有了一个全新的mvc webapp和一个如下所示的数据库: 谢谢你的帮助 请再问一个问题: 根据 上面说,Movie类包含一个Id字段,数据库需要该字段作为主键。 但是,我的数据库中没有整数ID字段,这导致在

我是asp.net核心的新手,随后我构建了一个新的mvc webapp,但它使用了一个新的SQLite数据库

我发现很多教程都使用SQLServer和VisualStudio。如何使用现有数据库在
Visual Studio code
中搭建脚手架?或者我怎样才能像教程一样做同样的事情

我现在有了一个全新的mvc webapp和一个如下所示的数据库:

谢谢你的帮助

请再问一个问题:

根据

上面说,
Movie类包含一个Id字段,数据库需要该字段作为主键。

但是,我的数据库中没有整数ID字段,这导致在尝试使用
aspnet codegenerator
构建控制器时出现错误
primary key not found
。有什么解决办法吗?谢谢大家!

如何使用现有数据库在VisualStudio代码中进行脚手架构建

如果要重用现有数据库,可以按以下方式执行:

  • 确保已添加这些必需的工具/包

    • dotnet ef
      工具:
      dotnet工具安装——全局dotnet ef
    • 其他相关包:
      • Microsoft.EntityFrameworkCore.Design
      • Microsoft.EntityFrameworkCore.SqlServer
      • Microsoft.VisualStudio.Web.CodeGeneration.Design
    如果您使用的是3.1,则可以通过cli安装这些软件包

    VSCode
    内的Ctrl+`键打开终端:

    > cd <the-folder-of-your-proj>
    > dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.1.*
    > dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.1.*
    > dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.1.*
    
    然后在
    Models/
    文件夹下生成一个
    MyDbContext
    文件和一些实体文件

  • 连接字符串
    从源代码移动到配置文件

    3.1清除
    MyDbContext
    中的onconfig()方法:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings. optionsBuilder.UseSqlServer("your-connection-string"); } } (注意这里我们使用双斜杠(
    \\
    )表示
    \
    已转义。这与第一步的powershell命令不同。)

  • 最后,在Startup::ConfigureServices()中添加一个
    DbContext
    服务:

    这是

    • $model
      是您的型号名称
    • $dcClass
      是您的DbContext类名
    • $controllerName
      是您的控制器名称
    • $controllerNamespace
      是名称空间
    如果
    $dcClass
    不存在,此命令还将添加新的DbContext。请随时更新
    application.json
    中的连接字符串(在这种情况下,不要忘记调用
    dotnet ef migrations add initial
    &&
    dotnet ef database update
    ,以便在运行MVC应用程序之前同步数据库)

    这个命令很乏味。因此,我编写了一个powershell命令来简化我的工作:

    param(
    [开关]$api=$false,
    [string]$model=$(读取主机“model Name(无命名空间)”)
    )
    if([string]::IsNullOrEmpty($model)){
    Write Host“您需要指定模型名称(不带命名空间)”
    出口
    }
    函数提示参数{
    param(
    [参数(必需=$true)][字符串]$message,
    $defaultValue
    )
    $result=Read Host“按enter$消息(默认值['$defaultValue'])”
    if([String]::IsNullOrEmpty($result))
    {
    $result=$defaultValue
    }
    返回$result
    }
    写入主机“[+]设置类名:”
    $controllerName=$(提示参数“控制器名称(不带命名空间)”-defaultValue“$($model)控制器”)
    $dcName=$(提示参数“DbContext名称(无命名空间)”-defaultValue“AppDbContext”)
    写入主机“[+]设置命名空间:”
    $rootNamespace=$(提示参数-消息“根命名空间”-默认值“应用”)
    $controllerNamespace=$(提示参数“控制器的命名空间”-默认值“$rootNamespace.Controllers”)
    $modelnespace=$(提示参数“模型的命名空间”-defaultValue“$rootNamespace.Models”)
    $dcNamespace=$(提示参数“DbContext的命名空间”-defaultValue“$rootNamespace.Data”)
    $modelClass=“$modelnespace.$model”
    $controllerClass=“$controllerNamespace.$controllerName”
    $dcClass=“$dcNamespace.$dcName”
    写入主机$rootNameSpace
    写入主机$modelClass
    写入主机$controllerClass
    写入主机$dcClass
    #是生成带视图的控制器还是生成ApiController
    $apiSwitch=if($api){“-api”}else{''}
    dotnet aspnet codegenerator控制器$apiSwitch-m$model-dc$dcClass-name$controllerName-namespace$controllerNamespace-outDir控制器--useDefaultLayout
    
    使用此
    dotnet controller generate.ps1
    ,我可以通过以下方式为模型生成控制器/视图:

    dotnet-controller-generate.ps1模型名
    
    通过以下方式为模型生成WebAPI:

    dotnet-controller-generate.ps1-api模型名
    
    如何使用现有数据库在VisualStudio代码中进行脚手架构建

    如果要重用现有数据库,可以按以下方式执行:

  • 确保已添加这些必需的工具/包

    • dotnet ef
      工具:
      dotnet工具安装——全局dotnet ef
    • 其他相关包:
      • Microsoft.EntityFrameworkCore.Design
      • Microsoft.EntityFrameworkCore.SqlServer
      • Microsoft.VisualStudio.Web.CodeGeneration.Design
    如果您使用的是3.1,则可以通过cli安装这些软件包

    VSCode
    内的Ctrl+`键打开终端:

    > cd <the-folder-of-your-proj>
    > dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.1.*
    > dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.1.*
    > dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.1.*
    
    然后在
    Models/
    文件夹下生成一个
    MyDbContext
    文件和一些实体文件

  • 连接字符串
    从源代码移动到配置文件

    3.1清除
    MyDbContext
    中的onconfig()方法:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings. optionsBuilder.UseSqlServer("your-connection-string"); } } (注意这里我们使用双斜杠(
    \\
    )来表示
    services.AddDbContext<MyDbContext>(opts=>{
        opts.UseSqlServer(Configuration.GetConnectionString("MyDbContext"));
    });