如何在asp.net核心1.1中更新现有的dbcontext

时间:2017-01-26 06:23:03

标签: c# entity-framework asp.net-core

我使用下面的命令来创建dbcontext

Scaffold-DbContext "Data Source=SANPC;Initial Catalog=EMAKITI;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data

上面的命令成功创建了包含所有表的dbcontext。

现在,我更改了一些表格,并使用以下命令更新

Scaffold-DbContext -Connection "Data Source=SANPC;Initial Catalog=EMAKITI;Integrated Security=True;Trusted_Connection=True;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entity -context EmakitiContext -Project DataBase -force

上面的命令给我一个错误

  

指定的deps.json [D:\ GitAlpha \ eMikitiWebApi \ eMikitiWebApiPortal \ src \ eMikitiWebApiPortal \ bin \ Debug \ netcoreapp1.1 \ eMikitiWebApiPortal.deps.json]不存在   处理以非零退出代码

结束

我做了很多研究。无法解决这个问题。我使用的是asp.net核心1.1

这是project.json文件

    {
  "dependencies": {
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
    "DataBase": "1.0.0-*",
    "UnitOfWork": "1.0.0-*",
    "ViewModel": "1.0.0-*",
    "Common": "1.0.0-*",
    "Microsoft.AspNetCore.Session": "1.1.0",
    "Microsoft.Extensions.Caching.Memory": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0",
    "System.IdentityModel.Tokens.Jwt": "5.1.2",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "runtimes": {
    "win10-x64": {}
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

1 个答案:

答案 0 :(得分:0)

您正在使用代码优先方法,并且在此方法中将流程从代码更改为数据库。实体框架工作为我们提供了从逆向工程从现有数据库开始的选项。当我们做一次逆向工程来生成我们的模型之后,我们需要遵循代码中的迁移流程,先将方法代码转换为数据库。

使用此命令我们可以生成迁移

dotnet ef migrations add InitialCreate -c SchoolContext

并将此应用于数据库。

dotnet ef database update -c SchoolContext

您可以详细了解此here