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

喜夏-厌秋 提交于 2019-12-05 11:11:15

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!