宇宙超能无敌之饼干怪

首页 新随笔 联系 管理

如何通过efcore生成实体模型

To create an Entity Data Model from the database using the Database First approach in EF Core, we’ll follow these steps:

  • Create a new console application project
  • Create a Model using the Scaffold-DbContext Command

Creating a new console application project

In this example, we’ll use Visual Studio 2019. If you don’t have a copy of Visual Studio 2019 installed in your system, you can download a copy from here: https://visualstudio.microsoft.com/downloads/

If Visual Studio 2019 has been successfully installed in your system, follow the steps outlined below to create a new console application project in Visual Studio.

  • Open Visual Studio 2019
  • On the File menu, click on New > Project
  • In the "Create a new project" dialog, select "Console App (.NET Core)"
  • Click Next
  • Now specify the name and location of the project
  • Click Create as shown in Figure 1

This would create a new console application project in Visual Studio 2019. We'll use this project in the subsequent sections.

Installing the necessary packages

Now that the database has been restored, specify the following commands at the NuGet Package Manager console to install the necessary packages.

Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.SqlServer			

Create a Model from the database

So far so good. To create the Entity Data Model, we'll take advantage of the Scaffold-DbContext command. Here's how the syntax of this command looks like:

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] 
                   [-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]			

To create the Entity Data Model for our AdventureWorks database, specify the following command at the Package Manager Console.

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

参文地址 www.mssqltips.com

posted on 2024-08-21 07:49  lazycookie  阅读(3)  评论(0编辑  收藏  举报