Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
1.3k views
in .Net Framework by 39 42 54
edited by

I am currently developing an ASP.NET Core application and need to create C# classes based on SQL database tables hosted on a remote server.

Therefore, I am planing to utilize an Object-Relational Mapping (ORM) technology, with Entity Framework core being one of the well-established ORM frameworks for the .NET ecosystem.

Could you guide me what's the best approach to create C# Classes from an SQL Database though Database First?


1 Answer

1 like 0 dislike
by 39 42 54
 
Best answer

Create C# Classes from SQL Database Schema in MVC using Scaffolding

To generate Classes from an SQL Database Schema, Here are the steps to achieve this:

  1. Install Entity EntityFrameworkCore in your application

     Install-Package EntityFrameworkCore
    
  2. Navigate to Tools -> NuGet Package Manager -> Package Manager Console
  3. Run below script

     Scaffold-DbContext "Server=tcp:SERVER_IP,SERVER_PORT;Initial Catalog=YOUR_DB;Persist Security Info=False;User ID=DB_USER;Password=DB_PASSWORD;Connect 
     Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
    
  • SERVER_IP : For example 172.171.14.1
  • SERVER_PORT: For example 5060
  • YOUR_DB: For example CatalogDB
  • DB_USER: For example userAdmin
  • DB_PASSWORD: For example m@#123%$
  • OutputDir: The Location of the Generated Classes Directory
If you don’t ask, the answer is always NO!
...