Asp.net Core EF options.UseInMemoryDatabase System.TypeLoadException

asp.net-core entity-framework-core

Question

I used EF in Asp.net Core, but got below error in below code:

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

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

}

Error Message:

An exception of type 'System.TypeLoadException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code

Additional information: Could not load type 'Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

Here is my 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%" ]
  }
}

Any help would be appreciated.
Reards,
Edward

1
9
3/1/2017 10:17:41 AM

Accepted Answer

After trying, I changed "1.0.0-rc2-final" to "1.1.0" which I already have tried, but I found there is an warning, "Dependency conflict. CoreMVCWebAPI 1.0.0 expected Microsoft.EntityFrameworkCore.InMemory >= 1.1.0 but received 1.0.0-rc2-final", after install this package manually instead of only changing project.json, it works now.

Install-Package Microsoft.EntityFrameworkCore.InMemory
26
3/1/2017 10:29:14 AM

Popular Answer

I just wanted to add that I had to restart VS2017 before the package restore was recognised. Prior to that I was getting an error

DbContextOptionsBuilder does not contain a definition for UseInMemoryDatabase() ...

I hope this saves you time.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow