Auto Generate Dbcontext Class From Database In ASP.NET Core 3.1

Auto Generate Dbcontext Class From Database In ASP.NET Core 3.1

In this article, we will see how to auto-generate Dbcontext class for migration in asp.net core 3.1. Friends as we know, . net core 3.1 uses the code first approach for the Database connection. We can create simple tables from the code first approach using migration and can create explicit Dbcontext.

In this article, we will see how to auto-generate Dbcontext class for migration in asp.net core 3.1. Friends as we know, .net core 3.1 uses the code first approach for the Database connection. We can create simple tables from the code first approach using migration and can create explicit Dbcontext.cs for 1 or 2 tables, or a few tables. But when we have a complex database of the project having multiple foreign key relationships with multiple tables then it is much more difficult for creating a database using migration. So we want to generate our project database from a Database first approach and want to maintain it from migration easily. This problem can be solved by just a single command of Entity Framework Core tool.

Before applying migration in your project, make sure migration is already enabled, and all the packages have been installed in your project

Go to Nu Get Package Manager > install this package

Now, I have already created my complex database in SQL server with the name "db_Demo" , which has multiple tables with a foreign key relationship, so let's auto-generate its table model and a DbContext class. 

Go to View Menu> Other Windows> Package Manager Console > Write this Commond  

  1. Scaffold-DbContext  "Server=MYSERVERNAME;Database=db_Demo;Trusted_Connection=True;"  Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database -Context  "ApplicationDbContext"  -DataAnnotations    

 Parameter 1

  1. Scaffold-DbContext- Keyword for  scaffold database  

 Parameter 2

  1. "Server=MYSERVERNAME;Database=db_Demo;Trusted_Connection=True;"   -Connection String     

Parameter 3

  1. Microsoft.EntityFrameworkCore.SqlServer -Package should be installed in  your project  

 Parameter 4

  1. -OutputDir - The command that specifies the output directory name where all models created   

 Parameter 5

  1. Database- Folder Name    

 Parameter 6

  1. ApplicationDbContext - Name of DbContext class   

 If any modification performs in the database then alter models by this command.

  1. Scaffold-DbContext  "Server=MYSERVERNAME;Database=db_Demo;Trusted_Connection=True;"  Microsoft.EntityFrameworkCore.SqlServer -OutputDir Database -Context  "ApplicationDbContext"  -DataAnnotations  -Force  

Output

database

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Benchmarking the Mainstream Open Source Distributed Graph Databases

The deep learning and knowledge graph technologies have been developing rapidly in recent years. Find out more about graph databases.

Database Design Tutorial - How to Design & Plan Database for Beginners

Learn how to design and plan a database for beginners. This database design course will help you understand database concepts and give you a deeper grasp of database design. Database design is the organisation of data according to a database model. The designer determines what data must be stored and how the data elements interrelate. With this information, they can begin to fit the data to the database model.

AlaSQL in Action: The JavaScript SQL Database

Overview on AlaSQL, the popular lightweight client-side in memory SQL database, including a real life example of AlaSQL in action. I was surprised to see that there aren’t more posts about this popular lightweight client-side in-memory SQL database online apart from this awesome article I found.

What is database continuous integration?

Have you ever longed for a way of making the delivery of databases more visible, predictable and measurable? Do you ever wish that they would be of better quality, quicker to change, and cost less? Grant Fritchey explains some of the secrets of doing Continuous Integration for Databases to relieve some of the pain-points of the Database Delivery process.

How to Build a Pokedex React App with a Slash GraphQL Backend

Take a look at how you can use use GraphQL to create a demo application of a Pokemon Pokedex app with React.