简体   繁体   English

OR/M 在 .NET Core Web API 项目中可用吗?

[英]Is OR/M available in .NET core web API project?

In ASP.NET Framework projects one could right click the project and add a ADO.NET Entity Data Model using an existing database.在 ASP.NET Framework 项目中,可以右键单击该项目并使用现有数据库添加 ADO.NET 实体数据模型。 This would create classes and properties from database's tables and columns.这将从数据库的表和列创建类和属性。

But this option is missing from ASP.NET Core API web application project.但是 ASP.NET Core API Web 应用程序项目中缺少此选项。 What can I use to create these classes from existing SQL Server database, or is that not possible?我可以用什么来从现有的 SQL Server 数据库创建这些类,或者这是不可能的?

VS 添加 ORM 到项目

Try to execute the following command under your project folder:尝试在您的项目文件夹下执行以下命令:

$ dotnet ef dbcontext scaffold "Server=....." Microsoft.EntityFrameworkCore.SqlServe
r -o Models

And fill the string Server=..... with your database connection string.并用您的数据库连接字符串填充字符串Server=..... Make sure that you can connect to this database successfully.确保您可以成功连接到此数据库。

命令

And EF will try to build models from the database. EF 将尝试从数据库构建模型。

生成

Reference: https://anduin.aiursoft.com/post/2020/2/13/creating-a-model-for-an-existing-database-in-entity-framework-core参考: https : //anduin.aiursoft.com/post/2020/2/13/creating-a-model-for-an-existing-database-in-entity-framework-core

This worked这有效

In Package Manager Console:在包管理器控制台中:

  1. Install-Package Microsoft.EntityFrameworkCore.Tools
  2. scaffold-dbcontext "your_connection_string" Microsoft.EntityFrameworkCore.SqlServer -outputdir Models -Table table1,table2

If you get an error that "The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet...", see this question: The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program如果您收到“未将术语 'scaffold-dbcontext' 识别为 cmdlet 的名称...”的错误,请参阅此问题:术语 'scaffold-dbcontext' 未识别为 cmdlet 的名称,函数、脚本文件或可运行的程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要创建一个我的 .Net Core 项目可以连接到的 .Net Web API 项目,但我使用的是 Mac - I need to create a .Net Web API project that my .Net Core project can connect to, but I'm on a Mac How to add .net core Web API project to an existing .NET core (3.1) Web Application project? - How to add .net core Web API project to an existing .NET core (3.1) Web Application project? 如何在 .NET 核心 5 Web ZDB974238714CA8DE634A7CE1D083A1 项目中自动格式化 API 响应? - How to autoformat API response in .NET core 5 Web API project? .Net Core Web API - .Net Core Web API 从另一个项目初始化ASP.NET Core Web API - Initialize ASP.NET Core Web API from another project net core web api 项目的类库中的依赖注入 - Dependency Injection inside class library for a net core web api project 在ASP.NET Core Web API项目中保持控制器的温暖 - Keeping controllers warm in ASP.NET Core Web API project 如何在 ASP.NET Core Web API 项目中设置 cookie - How to set a cookie in a ASP.NET Core Web API project CORS 在 ASP.NET 核心 Web API 项目中不工作 - CORS not working in ASP.NET core Web API project ASP.NET 核心 Web API - 项目参考中的 ConnectionString - ASP.NET Core Web API - ConnectionString in project reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM