ASP.NET-CORE-MVC – Error running Crud Scaffolding for DotNET Core ASP.NET

So I’m attacking .net core and launched a new asp.net core application. I created a .net core class library and included a reference to the Entity Framework core . I created a context and I tried to use this context in my asp.net core project. When I tried scaffolding, I kept getting this error:

enter image description here

For search purposes, here is 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 for other asp.net network projects many times in the past, and I always encounter problems when trying to abstract out the dbContext outside of the application project. However, I cannot find a solution this time.

Has anyone seen this error? I can provide more details about my project.json file, but instead of putting a big copy and paste in this article, I think I will first see if anyone has encountered this problem.

< p>Currently running on my website project:

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",< br /> "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.Entit yFrameworkCore.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"
},< br /> "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-p review2-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
},

"run timeOptions": {
"configProperties": {
"System.GC.Server": true
}
},

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

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

As a comment, I tried Configured my dbContext project as a .netcore application to try to make the migration work. I get the same error. I have currently reverted these changes and now set the dbContext project to .net standard.

This Is my dbContext project setting:

{
"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 think I should also say that I can run DbContext completely. I have run the migration on the database and can actually access the data .

I ran into the same problem. As you discovered, when you put the data model And DbContext can cause this problem when placed in a separate project (class library) of the web application. I believe this is a bug in the current ASP.NET Core. I don’t know if it will be fixed, but I found a reasonable The solution.

When you try to add a new scaffold item in Visual Studio, you will get the “Add Controller” dialog box. The key here is not to select DbContext from the class library As a 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 call it TempDbContext. The scaffolding will Successfully completed. If you want to build multiple model classes, please do this now; it will continue to use the same TempDbContext.

enter image description here

After completing the scaffolding operation, delete TempDbContext from the web application and replace the correct DbContext reference from the class library. Find the following to fix Project:

>In the new scaffolded Controller, fix the DbContext type name, no matter where it appears> Delete the TempDbContext file in the Data folder> Delete the TempDbContext connection string from appsettings.json> From Delete the AddDbContext line in the ConfigureServices method in Startup.cs

So I am attacking .net core and started a new asp.net core application. I created one .net core class library, and contains a reference to the Entity Framework core. I created a context, and I tried to use this context in my asp.net core project. When I tried scaffolding, I kept getting this error:

enter image description here

For For search purposes, here is 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 many times in the past for other asp.net network projects, and I am trying to abstract out the application I always encounter problems with dbContext outside of the program project. However, I cannot find a solution this time.

Has anyone seen this error? I can provide more details about my project.json file, but instead of putting a big copy and paste in this article, I think I will first see if anyone has encountered this problem.

< p>Currently running on my website project:

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",< br /> "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.EntityFr ameworkCore.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"
},< br /> "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-prev iew2-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
},

"runtim eOptions": {
"configProperties": {
"System.GC.Server": true
}
},

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

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

As a comment, I tried Configured my dbContext project as a .netcore application to try to make the migration work. I get the same error. I have currently reverted these changes and now set the dbContext project to .net standard.

This Is my dbContext project setting:

{
"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": {
"ne tstandard1.6": {}
}
}

I think I should also say that I can run DbContext completely. I have run the migration on the database and can actually access the data.

I encountered the same problem. As you discovered, when you put the data model and DbContext in a separate project of the web application (class library ) Will cause this problem. I believe this is a bug in the current ASP.NET Core. I don’t know if it will be fixed, but I found a reasonable solution.

When you try to add a new scaffold item in Visual Studio, you will get the “Add Controller” dialog. The key here is not to select DbContext as the data context class from the class library. 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 call it TempDbContext. The scaffolding will complete successfully. If you want to build multiple model classes, please Do this now; it will continue to use the same TempDbContext.

enter image description here

After completing the scaffolding operation, delete TempDbContext from the web application and replace the correct DbContext reference from the class library. Find the following items to be repaired:

>In the new In scaffolded Controller, fix the DbContext type name, no matter where 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

Leave a Comment

Your email address will not be published.