Asp.net核心EF選項.UseInMemoryDatabase System.TypeLoadException

asp.net-core entity-framework-core

我在Asp.net Core中使用了EF,但在下面的代碼中得到了以下錯誤:

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

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

}

錯誤信息:

Microsoft.EntityFrameworkCore.dll中出現“System.TypeLoadException”類型的異常,但未在用戶代碼中處理

附加信息:無法從程序集“Microsoft.Extensions.DependencyInjection.Abstractions,Version = 1.1.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60”加載類型“Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions”。

這是我的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%" ]
  }
}

任何幫助,將不勝感激。
Reards,
愛德華

一般承認的答案

嘗試之後,我將“1.0.0-rc2-final”更改為“1.1.0”,我已經嘗試了,但我發現有一個警告,“依賴衝突.CoreMVCWebAPI 1.0.0期望Microsoft.EntityFrameworkCore.InMemory> = 1.1.0但收到了1.0.0-rc2-final“,在手動安裝此軟件包後,而不是僅更改project.json,它現在可以正常工作。

Install-Package Microsoft.EntityFrameworkCore.InMemory

熱門答案

我只想補充一點,我必須在識別包恢復之前重啟VS2017。在此之前,我收到了一個錯誤

DbContextOptionsBuilder不包含UseInMemoryDatabase()的定義...

我希望這能節省你的時間。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow