简体   繁体   中英

Create Entity Framework model based on an existing database in ASP.NET Core

I'm trying to create an Entity Framework model based on an existing database in ASP.NET Core. I can't make Scaffold-DbContext to work though! I have searched Stackoverflow and other websites to see if the packages in my ASP.NET Core Web Application project are not correct but I still can't make it work. I get the following error when running the Scaffold-DbContext in Package Manager Console:

PM> Scaffold-DbContext "Server=.;Database=TravelAgency;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
System.AggregateException: Ett eller flera fel har uppst�tt. ---> System.MethodAccessException: Ett f�rs�k av metoden Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal.ModelConfiguration.get_EntityConfigurations() 
att komma �t metoden Microsoft.EntityFrameworkCore.Metadata.Internal.EntityTypeNameComparer..ctor() misslyckades.    vid Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal.ModelConfiguration.get_EntityConfigurations() 

Sorry, I didn't find the way to display the errors in Package Manager Console in English. It's about System.AggregateException: One or more errors occurred. My project.json-file looks like this:

{
  "buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
  },
  "dependencies": {
"EntityFramework": "6.1.3",
"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": {
  "type": "build",
  "version": "1.0.0-preview2-final"
},
"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": "1.1.0",
"Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"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.1.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": {
  "type": "build",
  "version": "1.0.0-preview2-final"
},
"NuGet.CommandLine": "3.5.0"
  },
  "frameworks": {

"net461": {}

  },

  "publishOptions": {
"include": [
  "wwwroot",
  "**/*.cshtml",
  "appsettings.json",
  "web.config"
]
  },
  "runtimeOptions": {
"configProperties": {
  "System.GC.Server": true
}
  },
  "scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },
  "tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.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"
  ]
}
  },
  "userSecretsId": "aspnet-TravelAgencyApplication-3fa3cffa-93a0-470d-b08a-145e604eb5f7"
}

Thanks in advance! Best Regards, Attila

You should try running the following command.

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Here is the official documentation on reverse engineering a db into your models. Make sure you have the correct packages installed per this link.

I have had a lot of issues with making this work with .net core from the package manager console. I ended issuing the command from the command line, which I had more success with. See here on how to do this: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet .

as an example, run the following from your project directory:

dotnet ef dbcontext scaffold "Server=YourServer;Database=YourDb;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer --output-dir Models

An issue was listed here, its now close but I still seemed to have issues: https://github.com/aspnet/EntityFramework/issues/5376

Thanks everyone! I don't know exactly what I did, but I finally succeeded with Scaffold-DbContext command:

Scaffold-DbContext "Server=DATOR;Database=TravelAgency;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

which is a command that destructi6n described, adjusted to my SQL Server 2012 database. Now my project.json-file looks like this:

{
  "buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
  },
  "dependencies": {
"EntityFramework": "6.1.3",
"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": {
  "type": "build",
  "version": "1.0.0-preview2-final"
},
"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": "1.1.0",
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
"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.1.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": {
  "type": "build",
  "version": "1.0.0-preview2-final"
},
"NuGet.CommandLine": "3.5.0"
  },
  "frameworks": {

"net461": {}

  },

  "publishOptions": {
"include": [
  "wwwroot",
  "**/*.cshtml",
  "appsettings.json",
  "web.config"
]
  },
  "runtimeOptions": {
"configProperties": {
  "System.GC.Server": true
}
  },
  "scripts": {
"prepublish": [ "bower install", "dotnet bundle" ],
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },
  "tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.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"
  ]
}
  },
  "userSecretsId": "aspnet-TravelAgencyApplication-3fa3cffa-93a0-470d-b08a-145e604eb5f7"
}

Kind Regards, Attila

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM