OR/M 在 .NET 核心 Web API 项目中可用吗?
Is OR/M available in .NET core web API project?
在 ASP.NET 框架项目中,可以右键单击项目并使用现有数据库添加 ADO.NET 实体数据模型。这将从数据库的表和列创建 类 和属性。
但是 ASP.NET 核心 API Web 应用程序项目缺少此选项。我可以使用什么从现有 SQL 服务器数据库创建这些 类,或者这不可能吗?
尝试在你的项目文件夹下执行以下命令:
$ dotnet ef dbcontext scaffold "Server=....." Microsoft.EntityFrameworkCore.SqlServe
r -o Models
并用您的数据库连接字符串填充字符串 Server=.....
。请确保您可以成功连接到此数据库。
并且 EF 将尝试从数据库构建模型。
这有效
在程序包管理器控制台中:
Install-Package Microsoft.EntityFrameworkCore.Tools
scaffold-dbcontext "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -outputdir Models -Table table1,table2
如果您收到 "The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet..." 的错误,请参阅此问题:The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program
在 ASP.NET 框架项目中,可以右键单击项目并使用现有数据库添加 ADO.NET 实体数据模型。这将从数据库的表和列创建 类 和属性。
但是 ASP.NET 核心 API Web 应用程序项目缺少此选项。我可以使用什么从现有 SQL 服务器数据库创建这些 类,或者这不可能吗?
尝试在你的项目文件夹下执行以下命令:
$ dotnet ef dbcontext scaffold "Server=....." Microsoft.EntityFrameworkCore.SqlServe
r -o Models
并用您的数据库连接字符串填充字符串 Server=.....
。请确保您可以成功连接到此数据库。
并且 EF 将尝试从数据库构建模型。
这有效
在程序包管理器控制台中:
Install-Package Microsoft.EntityFrameworkCore.Tools
scaffold-dbcontext "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -outputdir Models -Table table1,table2
如果您收到 "The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet..." 的错误,请参阅此问题:The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program