📜  .net framework core scaffhold 存在表 - TypeScript (1)

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

.NET Framework Core Scaffolding for Existing Tables - TypeScript

In .NET Core, scaffolding is a technique that can be used to generate code for common tasks such as creating database tables and Web APIs. By using scaffolding, you can automate much of the tedious work involved in creating new applications, freeing up valuable time that you can use to focus on more important tasks.

In this tutorial, we'll focus on using scaffolding to create TypeScript code for existing database tables. Specifically, we'll be using the Entity Framework Core command-line interface (CLI) to generate TypeScript classes that map to our existing database tables.

Prerequisites

Before we get started, there are a few things you'll need to have installed on your computer:

  • .NET Core SDK (version 3.1 or later)
  • Entity Framework Core tools (version 3.1 or later)
  • TypeScript (version 4.0 or later)

If you don't already have these items installed, you can download them from the following links:

Step 1: Create a .NET Core project

The first step in our journey is to create a new .NET Core project. To do this, open a terminal or command prompt and navigate to the directory where you want your project to reside.

cd /path/to/project/directory

Next, run the following command to create a new .NET Core project with the name of your choice.

dotnet new webapi -n MyApp

This will create a new .NET Core project with an API controller and a default WeatherForecast model.

Step 2: Connect to your existing database

Next, you'll need to connect your .NET Core project to your existing database. To do this, you'll need to update the appsettings.json file in your project's root directory.

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Replace the Server, Database, User Id, and Password values with your own database connection string.

Step 3: Scaffold your TypeScript classes

With your database connected, you can now scaffold your TypeScript classes for your existing tables. To do this, you'll need to use the Entity Framework Core CLI.

In your terminal or command prompt, navigate to your project's root directory and run the following command:

dotnet ef dbcontext scaffold "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f --context MyAppContext

This command will generate TypeScript classes for your existing database tables and add them to the Models directory in your project. It will also create a new MyAppContext class that you can use to interact with your database.

Step 4: Use your TypeScript classes

With your TypeScript classes generated, you can now start using them in your project. To do this, you can simply import the classes into your TypeScript files.

For example, if you generated a TypeScript class for a table named Orders, you could import it into a TypeScript file like this:

import { Orders } from '../Models/orders';

You can then use the Orders class in your code to interact with the corresponding database table.

Conclusion

Using scaffolding can be a powerful way to automate the generation of code for common programming tasks. In this tutorial, we've explored how to use the Entity Framework Core CLI to scaffold TypeScript classes for existing database tables in a .NET Core project. With these TypeScript classes, you can easily interact with your database and develop your application more efficiently.