【问题标题】:EF Migrations: The parameter 'frameworkName' cannot be an empty stringEF 迁移:参数“frameworkName”不能为空字符串
【发布时间】:2019-04-23 17:16:54
【问题描述】:

我正在关注一些关于如何使用 .netcore 设置我的第一个 Web api 的复数视图教程。我在尝试运行我的第一次迁移时遇到问题:

PM> Add-Migration InitialMigration Exception calling ".ctor" with "1" argument(s): "The parameter 'frameworkName' cannot be an empty string. Parameter name: frameworkName"

我明白这是说某处有一个构造函数,其参数为空;但是,我不知道这个“frameworkName”参数在哪里。我假设它是一些内部 EF 机制。

此错误消息引用的是什么类?

这是我的简单实体设置

public class ShackupContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
    public ShackupContext(DbContextOptions<ShackupContext> options):base(options)
    {

    }
}  


    public class Post
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    [MaxLength(200)]
    public string Name { get; set; }
}  

更新 1 我已经更新了我的 dbcontext 以按照@ViktorsTelle 的建议调用基类构造函数。

我的项目设置不同于我遵循的任何教程。主要区别在于我的实体包含在他们自己的项目中,而不是在 api 项目本身中。

这让我相信在 Shackup.Data 项目本身上运行 Add-Migration 不需要我在我的 api 项目中注册我的 dbcontext。我还是这样做了,看看会发生什么:

 public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.AddDbContext<ShackupContext>(o => 
        o.UseNpgsql(Configuration["connectionStrings:postgres"],
        a => a.MigrationsAssembly("Shackup.Data")));
    }  

一旦我这样做了,我就设法在迁移过程中走得更远。我现在收到
PM> Add-Migration InitialMigration The specified deps.json [C:\Users\campo\Documents\Visual Studio 2015\Projects\Shackup\src\Shackup.Api\bin\Debug\netcoreapp1.1\Shackup.Api.deps.json] does not exist Process finished with non-zero exit code

所以我查看了那个目录,确实缺少文件;但是,该文件正在输出到该目录的子文件夹。

deps.json 文件位于 win10-x64 文件夹内。我可以简单地复制并粘贴到所需的文件夹,但现在我面临一个新问题:

为什么 Nuget 包管理器控制台在错误的目录中查找?
或者可能:如何更改它以查看正确的位置?

更新 2

我已经更新了两个项目中的所有项目依赖项。下面是数据和api项目的两个文件

API

{
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.AspNetCore.Server.Kestrel": "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": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.NETCore.App": "1.1.0",
"Shackup.Data": "1.0.0-*",
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final"
},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
},

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

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

"runtimeOptions": {
"configProperties": {
  "System.GC.Server": true
}
},
"runtimes": {
"win10-x64": {},
"win81-x64": {}
},

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

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

数据

{
"version": "1.0.0-*",

"dependencies": {
"Microsoft.EntityFrameworkCore": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"NETStandard.Library": "1.6.1"
},

 "frameworks": {
"netstandard1.6": {
  "imports": "dnxcore50"
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
}
}

【问题讨论】:

  • 我注意到的第一件事是构造函数中缺少基类调用。请在构造函数中添加:base(options)。
  • 您使用的是哪个版本的 Visual Studio,尤其是 NuGet?您需要 VS 2015 Update 3NuGet v 3.5 才能使用 .NET Core EF 命令
  • 您好,我有 v. 3.5 nuget 以及 VS2015 Enterprise Update 3: 14.0.25431.01
  • 最后一次编辑,您不需要 Npgsql.EntityFrameworkCore.PostgreSQL.Design 吗?
  • 并且...将imports部分更改为依赖dnxcore50而不是dotnet5.6

标签: c# entity-framework nuget entity-framework-core


【解决方案1】:

在您的解决方案中选择适当的启动项目:在解决方案资源管理器中右键单击您的项目,选择“设置为启动项目”

【讨论】:

    【解决方案2】:

    选择您的实体包含的项目作为启动项目(在解决方案资源管理器中右键单击您的项目并选择“设置为启动项目”)并确保您安装了Microsoft.EntityFrameworkCore.Design 包。可能这不是最好的解决方案,但解决了我同样的问题。

    【讨论】:

      【解决方案3】:

      就我而言,全新的 .Net Core 2.2 MVC 应用程序通过选择启动对象解决了该问题:

      1. 右键单击您的应用程序
      2. 选择“属性”
      3. 在“应用程序”选项卡中,“启动对象”下拉列表的值为“(未设置)”。选择(仅)其他可用项目“MyAppName.Program”对我有用。

      【讨论】:

        猜你喜欢
        • 2019-08-19
        • 2021-07-26
        • 2019-05-13
        • 2017-12-30
        • 1970-01-01
        • 1970-01-01
        • 2018-09-24
        • 1970-01-01
        • 2018-03-04
        相关资源
        最近更新 更多