Generate entity framework core code first models from database in ASP.NET Core

In this tutorial i am going to explain about how to Generate entity framework core code first models from database in ASP.NET Core. In my previous tutorial i have explained about

Implementation :

Now we will see the implementation of how to Generate entity framework core code first models from database in ASP.NET Core.  While starting a new project sometime database may exist and if we can generate code first models from the existing database then it will reduce our effort. Now let’s see how to Generate entity framework core code first models from database in ASP.NET Core.

First open visual studio (i am using VS2017 community version) and create a new ASP.NET Core project.

Step 1 of Generate entity framework code first models from database in ASP.NET Core

 

 

And then for demo purpose i have created a sample table called Employee in my database. Below is the script.

Now go to Tools-> NuGet Package Manager -> Package Manager Console like shown in the below screenshot.

Step 2 of Generate entity framework code first models from database in ASP.NET Core

Now to Generate entity framework core code first models from database in ASP.NET Core Scaffold-DbContext command is used. Below is the syntax to run Scaffold-DbContext command.

Below is the sample command i have used to generate models from database.

It will generate the model calsses and DbContext file. Below is the generated Employee.cs & DemoContext.cs file contents.

Employee.cs

DemoContext.cs

Using Database Names:

Now if you look into the Employee.cs the table name and column name is not matching. My table name is EMPLOYEE but the class name is Employee. This is because by default database names are changed to c# name style conventions. If you want to keep the database names then you have to pass additional parameter -UseDatabaseNames in your command. Now the modified command look like

Now if i run the above command it throws the below error.

The following file(s) already exist in directory C:\Users\xxxx\Documents\Visual Studio 2017\Projects\Asp.NetCore2.0Application\Asp.NetCore2.0Application\Models: DemoContext.cs,Employee.cs. Use the Force flag to overwrite these files.

So we need to override the existing files. For that we need to pass -Force parameter int the command. So the modified command will be

If you run this command you will get the below class with names same like the database table.

Using Data Annotations:

After that let’s look into DemoContext.cs all the models are configured using FluentAPI. This is the default option. But if you want to use DataAnnotation attributes to configure the models then we need to pass -DataAnnotations parameter in the command like below.

Now you can see the modified Employee.cs and DemoContext.cs file contents.

Employee.cs

DemoContext.cs

Conclusion:

Hope you learned how to Generate entity framework core code first models from database in ASP.NET Core. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *