【问题标题】:Asp.net Core EF options.UseInMemoryDatabase System.TypeLoadExceptionAsp.net Core EF options.UseInMemoryDatabase System.TypeLoadException
【发布时间】:2017-03-01 10:17:41
【问题描述】:

我在 Asp.net Core 中使用了 EF,但在下面的代码中出现以下错误:

public class TodoContext : DbContext
{
    public TodoContext(DbContextOptions<TodoContext> options)
        : base(options)
    {

    }
    public DbSet<TodoItem> TodoItems { get; set; }

}

错误信息:

“System.TypeLoadException”类型的异常发生在 Microsoft.EntityFrameworkCore.dll 但未在用户代码中处理

附加信息:无法加载类型 'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions' 从程序集'Microsoft.Extensions.DependencyInjection.Abstractions, 版本=1.1.0.0,文化=中性,PublicKeyToken=adb9793829ddae60'。

这是我的 Project.json

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",

    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    //Dependence for MVC
    "Microsoft.AspNetCore.Mvc": "1.1.1",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    //Dependence for EF
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.InMemory": "1.0.0-rc2-final"
    //Dependence for EF with SQL, this is avalible under VS 2017 RC
    //"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    //Entity Framework commands to maintain the database
    //"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final"
  },

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

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

  "buildOptions": {
    "emitEntryPoint": true,
    //used for Razor pages which are compiled at runtime,and the compiler needs access to reference assemblies,
    //to make sure it compiles correctly
    "preserveCompilationContext": true
  },

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

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

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

任何帮助将不胜感激。
后备箱,
爱德华

【问题讨论】:

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


    【解决方案1】:

    尝试后,我将“1.0.0-rc2-final”更改为“1.1.0”,我已经尝试过了,但我发现有一个警告,“依赖冲突。CoreMVCWebAPI 1.0.0 预期 Microsoft.EntityFrameworkCore。 InMemory >= 1.1.0 but received 1.0.0-rc2-final”,在手动安装此包后,而不是只更改 project.json,它现在可以工作了。

    Install-Package Microsoft.EntityFrameworkCore.InMemory
    

    【讨论】:

    • 感谢@edward 它工作正常,但我已经安装了它应该包含在内存中的 EntityFrameworkCore。是不是。
    【解决方案2】:

    我只是想补充一点,在识别包还原之前我必须重新启动 VS2017。在此之前我遇到了一个错误

    DbContextOptionsBuilder 不包含 UseInMemoryDatabase() ...

    我希望这可以节省您的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2018-09-27
      相关资源
      最近更新 更多