Database First Approach in Entity Framework


Read my last article "Code First Approach in Entity Framework" for an EF introduction even for the basic level.

What is Database First Approach?

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model and it creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller. If you remember my last article "Code First Approach in Entity Framework" (I'll recommend you to read my "Code First" article, since there are many basics covered), where first we have created model codes (classes, properties, DbContext) and then these model classes create a database for us at run time and that's why we called it "Code First".

Demo MVC Application

Create a new ASP.NET MVC Project by New > Project > ASP.NET MVC 4 Web Application > Empty Template, follow the images:




Now, follow the steps.

Step 1: Adding NuGet Package (if not available in references)

Right-click on the References Folder and select "Manage NuGet Packages". Alternatively you can install it from Tools > Library Package Manager > Package Manager Console and type "Install-Package EntityFramework". Okay, let's do this from NuGet Package window, type "Entity Framework" in search box and click to install it. After installation you will notice a new library file in References Folder "EntityFramework".


Step 2: Adding Model1.edmx

Now, we have all the required NuGet libraries. Let's go ahead and add an "Entity Data Mode" file by right-clicking on the Model folder and select Add > New Item and in the window select "ADO.NET Entity Data Model", you can see additional steps in the image below. Remember to select the "Generate from database" option in the "Entity Data Model Wizard" and select any SQL Server Database or you may select Database file (.mdf). I'll be using Database file in this demo, since my database file is located at another location (not in the project) so, I'll be prompted to copy the database file into the project's "App_Data" folder. If you will select SQL Server or any other database vendor, you will not be asked to do so. Please refer to the image:



Now, you have an Entity Data Model file in your Model folder with all its necessary supportive files.


If you open the "Model1.Designer.cs" file, you will see all the properties, DbContext and DbSet that we need to establish a link between the model and the database, but you know these lines are a bit complicated or you can call it "not clean". Don't worry, we can create a cleaner version of this file. How?

You can do this by right-clicking on the "Model1.edmx" file and selecting "Add Code Generation Item" and from the window install "EF DbContext Generator for C#". Follow the image:



Now, you will see much cleaner code, like we saw in the "Code First" article. Look at the screen:


Now we are ready to add Controllers and appropriate Views in the project which will be exactly the same as we did in Step 3 to Step 7 of the article "Code First Approach in Entity Framework". So, go through the article and do the remaining code. I know I'm quitting here, because it is better to write another article than duplicating something.

I hope you like it. Thanks.

Up Next
    Ebook Download
    View all
    View all