Friday, November 10, 2017

Create Entity Framework Model classes through EF Core with Database First approach.

Today, we will walkthrough about .Net Core with EF Core (Data First approach). In this article, we will learn to create Entity Framework model classes with existing database.

Let's start.

Firstly, we will create a simple .Net Core console app. Currently, this console app has a Main() method under Program.cs class file. If you will run this project, it will print “Hello World!” on console window. This means, everything is running good.

Let us move ahead-

Now, we need to create an Entity Framework model classes and to do this, we need to install Nuget package for the database provider(s). In this demo, we will be using SQL Server 2016 however, in next articles, we will target the other database providers.

To install the Nuget package for SQL Server database provider
  • Open package manager console window and install the package
  • Install-Package Microsoft.EntityFrameworkCore.SqlServer
    
  • We have to install some Entity Framework tool(s) to create model classes from the database and to do so, we have to go in package manager console and run the command.
  • Install-Package Microsoft.EntityFrameworkCore.Tools
    
Now, that we have our complete setup with us, we are now ready to create model classes through reverse engineering. I already have a dummy database in SQL Server 2016, So we will directly move on creating model classes. Go into the package manager console and run the below command.

Scaffold-DbContext "ServerDATABASESERVER;Database=DATABASENAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer 

The above command will generate the model class in our project root folder. If you have (n) number of model classes then It will become quite difficult to manage all these classes, so in this case all model classes should be in a Model folder but our application does not have any folder as such, however we don’t have to worry because we can create this folder using above command i.e. If we append one parameter “OutputDir”. For example:

Scaffold-DbContext "ServerDATABASESERVER;Database=DATABASENAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir 

Now our Entity Framework model classes have been created under the Models folder. Now we have model classes against every table and a derived context based on schema of our database.
Is it possible?  If I want to create model class for specific database table or schema. Yes, it is!!
Microsoft provides the facility to specify the table and schema in Scaffold-DbContext command.
  • For tables
Scaffold-DbContext "ServerDATABASESERVER;Database=DATABASENAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Hotel
  • For schema
Scaffold-DbContext "ServerDATABASESERVER;Database=DATABASENAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Schemas Report

Remember, if you run the command with the table or schema which is incorrect (not exists) then command will give only warning and create only context class and If table has any foreign key relationship with other table and you are using only primary key table or foreign key table, in that case also, it will raise only warning not error.

Now, we have model classes and we can perform data operation on these model classes. Now, if someone comes in and changes the schema or add new table in your database, how will we update our context class and create a new model class against newly added table in database? For that:

We will use same command as we were using to create model class. Just add the “-Force” argument end of the command, it is ready to update your context classes.

Scaffold-DbContext "ServerDATABASESERVER;Database=DATABASENAME;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

By the help of these commands you can easily update your EF model classes. Remember, when you run any of the command, at that time there should not be any compile time error in your project. Otherwise, command will not be execute.  

1 comment:

Create Entity Framework Model classes through EF Core with Database First approach.

Today, we will walkthrough about .Net Core with EF Core (Data First approach). In this article, we will learn to create Entity Framework ...