基于 ASP.NET Core 中的现有数据库创建实体框架模型
Posted
技术标签:
【中文标题】基于 ASP.NET Core 中的现有数据库创建实体框架模型【英文标题】:Create Entity Framework model based on an existing database in ASP.NET Core 【发布时间】:2017-07-11 13:12:54 【问题描述】:我正在尝试。我不能让 Scaffold-DbContext 工作!我搜索了 *** 和其他网站,看看我的 ASP.NET Core Web 应用程序项目中的包是否不正确,但我仍然无法使其工作。在包管理器控制台中运行 Scaffold-DbContext 时出现以下错误:
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()
抱歉,我没有找到在包管理器控制台中用英文显示错误的方法。这是关于 System.AggregateException:发生了一个或多个错误。 我的 project.json 文件如下所示:
"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"
提前致谢! 最好的问候,阿提拉
【问题讨论】:
【参考方案1】:您应该尝试运行以下命令。
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.
【讨论】:
感谢 destructi6n,但您的意思是我应该创建一个本地数据库才能使其正常工作吗?我之前创建了一个 Sql Server 2012 数据库,但我似乎无法让 Scaffold-DbContext 工作,即使我使用了你提到的命令。 您应该做的是将我的连接字符串(即 Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;)替换为指向您的数据库的连接字符串。我之前在我的 localdb 实例中使用过这个命令。如果你提供你的连接字符串,这个命令应该对你有用。【参考方案2】:在包管理器控制台中使用 .net core 进行这项工作时,我遇到了很多问题。我结束了从命令行发出命令,我在这方面取得了更大的成功。请参阅此处了解如何执行此操作: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
【讨论】:
【参考方案3】:谢谢大家!我不知道我到底做了什么,但我终于用 Scaffold-DbContext 命令成功了:
Scaffold-DbContext "Server=DATOR;Database=TravelAgency;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
这是 destructi6n 描述的命令,已针对我的 SQL Server 2012 数据库进行了调整。现在我的 project.json 文件看起来像这样:
"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"
亲切的问候,阿提拉
【讨论】:
以上是关于基于 ASP.NET Core 中的现有数据库创建实体框架模型的主要内容,如果未能解决你的问题,请参考以下文章
更新 ASP.NET Core Entity Framework 中的实体类
如何在现有数据库 ASP.NET Core MVC 和 EF Core(DB-First)中使用 Identity
(17)ASP.NET Core EF基于数据模型创建数据库
创建基于ASP.NET core 3.1 的RazorPagesMovie项目-应用模型类配合基架生成工具生成Razor页面