Auto Generate Model Files(C#) from DB

Santhi Dhanuskodi 325 Reputation points
2023-07-26T12:01:57.34+00:00

Hi,

How can we generate c# Model files automatically. The DB can be SQL Server or Azure SQL .

Thanks

Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2023-07-27T02:46:37.5466667+00:00

    Hi @Santhi Dhanuskodi

    To generate models and DbContext automatically from the SQL server or Azure SQL database, we could use the Microsoft.EntityFrameworkCore.Design package and the Microsoft.EntityFrameworkCore.SqlServer database provider.

    You can refer to the following methods:

    Create an Asp.net core application and add the connection string in the appsettings.json file, like this:

    User's image

    Method 1: using .Net Core CLI command.

    1.Right click the project and select the "open in terminal" option.

    2.In the terminal window, use the following command to install the EF core tools:

    dotnet tool install --global dotnet-ef
    

    like this: User's image

    3.Use the following commands to install the Microsoft.EntityFrameworkCore.Design package and the Microsoft.EntityFrameworkCore.SqlServer database provider:

    dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.9
    dotnet add package Microsoft.EntityFrameworkCore.Design --version 7.0.9
    

    4.Finally, use the following command to generate the models and dbcontext:

    dotnet ef dbcontext scaffold "Name=ConnectionStrings:dbcontectstring" Microsoft.EntityFrameworkCore.SqlServer -o Models -c BlogContext 
    

    Method 2: Using the Package Manager Console

    1.Open the Package Manager Console and use the following commands to install the related package:

    NuGet\Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 7.0.9
    NuGet\Install-Package Microsoft.EntityFrameworkCore.Design -Version 7.0.9
    NuGet\Install-Package Microsoft.EntityFrameworkCore.Tools -Version 7.0.9
    

    2.Then, use the following command to add the Models and Context

    Scaffold-DbContext 'Name=ConnectionStrings:dbcontectstring' Microsoft.EntityFrameworkCore.SqlServer -context ApplicationDbContext -OutputDir Models
    

    The output like this:

    User's image

    More detail information about the .Net Core CLI or Scaffold-DbContext commands, see:

    Entity Framework Core tools reference - Package Manager Console in Visual Studio

    Entity Framework Core tools reference - .NET Core CLI


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-07-27T07:35:49.8+00:00

    Check out EF Power Tools which will generate all models, some models, include/exclude columns etc. Will even install EF Core required NuGet packages. And if you do a another process EF Power Tools remembers what you did before.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.