Create an Entity Framework Model Based on an Existing Database in the ASP.NET Core

I am trying to create an Entity Framework model based on an existing database in ASP.NET Core. I can't get Scaffold-DbContext to work! I searched Stackoverflow and other websites to see if the packages in my ASP.NET Core Web Application project are wrong, but I still can't get it to work. When launching Scaffold-DbContext in the package manager console, the following error appears:

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 did not find a way to display errors in the Package Manager console in English. This is about a System.AggregateException: one or more errors have 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! Regards, Attila

+4
3

.

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

db . , .

0

.net . , . . , : https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet.

:

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

, , : https://github.com/aspnet/EntityFramework/issues/5376

0

Thanks everyone! I don’t know exactly what I did, but I finally managed to execute the Scaffold-DbContext command:

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

which is the command described by destructi6n 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"
}

Regards, Attila

0
source

Source: https://habr.com/ru/post/1670455/


All Articles