c# - EF 迁移 : The parameter 'frameworkName' cannot be an empty string

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

我正在学习一些关于如何使用 .netcore 设置我的第一个 web api 的教程。我在尝试运行我的第一次迁移时遇到问题:

PM> 添加迁移 InitialMigration 使用“1”个参数调用“.ctor”时出现异常:“参数‘frameworkName’不能为空字符串。 参数名称: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 项目本身中。
enter image description here
这让我相信在 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> 添加迁移 InitialMigration 指定的 deps.json [C:\Users\campo\Documents\Visual Studio 2015\Projects\Shackup\src\Shackup.Api\bin\Debug\netcoreapp1.1\Shackup.Api.deps.json] 不存在 进程以非零退出代码结束

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

enter image description here

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"
}
}

最佳答案

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

关于c# - EF 迁移 : The parameter 'frameworkName' cannot be an empty string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41269395/

相关文章:

c# - WPF 指向路径几何的点

c# - 将空格附加到 stringbuilder 左对齐

asp.net-mvc - ASP.NET MVC 动态添加子记录

entity-framework - 模拟 Entity Framework 模型?

c# - EF N 层架构

c# - 从流中加载图像而不保持流打开

c# - 在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或 <serviceDebug> 配置行为)

nuget - 使用二进制内容文件创建 Nuget 包 - 这可能吗?

c# - Ninject.MVC5 不生成 NinjectWebCommon.Cs

c# - 在 NuGet 配置中使用环境变量?