📜  db scaffolding ef core - C# (1)

📅  最后修改于: 2023-12-03 15:14:37.696000             🧑  作者: Mango

Db Scaffolding with Entity Framework Core in C#

When building applications, developers often need to interact with a database. In many cases, they have to create the same database tables and models over and over again. This is where Db Scaffolding comes in handy. Db Scaffolding is a technique of automatically generating code for database access from the existing database schema.

Entity Framework Core is an open-source, cross-platform version of Microsoft's popular Entity Framework data access technology. It provides a way to interact with databases using ORM (Object-Relational Mapping) techniques. With the help of Entity Framework Core, developers can easily scaffold database models and make their database interactions more efficient.

In this article, we will discuss how to use Entity Framework Core to scaffold a database in C#. We will cover the following topics:

  • Setting up the environment
  • Creating a new database
  • Scaffolding the database
  • Modifying the scaffolded code
Setting up the Environment

Before starting with the actual scaffolding process, let's set up the environment. We will need the following:

  • Visual Studio or other IDE
  • .NET Core SDK
  • Entity Framework Core Package (NuGet)

After installing these tools, create a new C# project in Visual Studio.

Creating a new database

In this article, we will use the Northwind sample database. You can download it from the official Microsoft website or use any other database you like. Once you have the database, create a new connection in Visual Studio that points to the database.

Scaffolding the database

To scaffold the database, we will use the Entity Framework Core tools. These tools are provided as a package in NuGet. Open the Package Manager Console in Visual Studio and install the package by running the following command:

Install-Package Microsoft.EntityFrameworkCore.Tools

Once the package is installed, we can use the following command to scaffold the database:

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

Replace ServerName and DatabaseName with your actual values. The command will generate models and a DbContext class in the specified output directory.

Modifying the scaffolded code

The generated code may need some modifications to fit your needs. For example, you may want to add some custom validation or add additional properties to the models.

Open the generated DbContext class and add your custom code. For example, you may want to add a DbSet for a custom model:

public DbSet<ProductReview> ProductReviews { get; set; }

You can then scaffold the new model by running the command again:

Scaffold-DbContext "Server=ServerName;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context NorthwindContext -Tables ProductReview

The command will generate a new model for the ProductReview table and add it to the existing DbContext class.

Conclusion

In this article, we discussed how to scaffold a database using Entity Framework Core in C#. We covered setting up the environment, creating a database connection, scaffolding the database, and modifying the scaffolded code. With the help of Entity Framework Core and Db Scaffolding, developers can easily interact with databases and save time on repetitive code.