4
votes

So I am taking a stab at .net core and have started a new asp.net core application. I have created a .net core class library and have included a reference to entity framework core. I have created a context and I am trying to scaffold with this context in my asp.net core project. I keep running into this error when I try to scaffold:

enter image description here

For searching purposes here's the error message:

There was an error running the selected code generator:

'There was an error creating/modifying a DbContext, there was no type returned after compiling the new assembly successfully.'

I have used entity framework multiple times in the past for other asp.net net projects and I have ALWAYS had issues when attempting to abstract out the dbContext outside of the application project. However, this time I cannot figure out a workaround for this.

Has anyone seen this error? I can provide more details about my project.json files but instead of putting a big copy paste in this post I thought I would start with seeing if anyone has run into this.

Currently running on my web project:

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": {
      "version": "1.0.1",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
        "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
            "version": "1.0.0-preview2-final",
            "type": "build"
        },
        "DBCONTEXTPROJECT: "1.0.0-*"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    }
  },

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

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

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

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

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

As a note I have attempted to configure my dbContext project as a .netcore app in attempts to get migrations to work as well. I get the same error either way. I have currently reverted those changes back and now have the dbContext Project set up as .net standard.

This is my dbContext project setup:

{
    "version": "1.0.0-*",

    "dependencies": {
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
        "NETStandard.Library": "1.6.0"
    },

    "tools": {
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
    },
    "frameworks": {
        "netstandard1.6": {}
    }
}

I suppose I should also state that I can run the DbContext perfectly fine. I have run the migration on the database and can actually access the data.

2
you have to show the project.json file ?Sampath
Updated to include both project.json files.kmacdonald
I don't see a reference to your class library in the web application project.json. Did you remove it?Doug
This error still occurs in Visual Studio 2017, just released.Doug

2 Answers

0
votes

I was having the same issue. The problem is caused when you have your data model and DbContext in a separate project (class library) from your web application, as you've discovered. I believe this is a bug in the current ASP.NET Core. I don't know if it will get fixed, but I've found a reasonable workaround.

When you try to add a new scaffolded item in Visual Studio, you get the "Add Controller" dialog. The key here is to NOT select the DbContext from your class library as the data context class. Instead, click the "plus" button to create a (temporary) New Data Context in the web application project (see image below). It doesn't matter what you name it, but I'll call it TempDbContext. The scaffolding will successfully complete. If you want to scaffold multiple model classes, go ahead and do so now; it will continue to use the same TempDbContext.

enter image description here

When you're done scaffolding, remove the TempDbContext from the web application and replace with the correct DbContext reference from the class library. Look for the following items to fix:

  • In the new scaffolded Controller, fix the DbContext type name wherever it appears
  • Delete the TempDbContext file in the Data folder
  • Delete the TempDbContext connection string from appsettings.json
  • Delete the AddDbContext line from the ConfigureServices method in Startup.cs
0
votes

You can do Right click controllers folder=> Add New Item => Webapi controller/etc.

my problem is solved here for controllers with read/write actions