Database First Approach With ASP.NET MVC Step By Step Part 2

In previous part of this series, you learned how to create database for the ASP.NET MVC application. In this part, we will see how to create model classes from database using Edmx file.

To create new project, Open Visual Studio, I am using Visual Studio 2013. Click File Menu, then New and choose Project.

START PAGE

It will open a new window from where you can choose the application type. Choose Web from Installed and then choose ASP.NET Web Application. Give it proper project name and also provide the location of project to save and click on OK.

NEW PROJECT

This will open a dialog where we can choose New ASP.NET Project, So, here we are going to choose an MVC, and choose OK.

NEW ASP.NET PROJECT

So, finally we have created an ASP.NET MVC application. When you go through the Solution Explorer, project structure will look like the following. Here we can see different type of folders and files, such as Controllers, Models, etc.

SOLUTION EXPLORER

So, we have created an MVC project. Now it is time to create models from existing database.

To Add Models, Right Click on Models folder and choose Add and then choose New Item.

NEW ITEM

It will open a dialog where we need to choose Data node then ADO.NET Entity Data Model. Provide the valid name and chick OK.

ADD NEW ITEM

In the Entity Data Model Wizard, select EF Designer from database and click Next.

EF DESIGNER

Click the New Connection button where you will define the database connection.

CHOOSE YOUR DATA CONNECTION

In the Choose Data Source window, we need to choose Data source and click Continue.

CHOOSE DATA SOURCE

It will open a new dialog where you need to specify everything about database connection. Here we will pass the user name and password and also choose the database. We can also check our database connection to click on Test Connection.

DATA SOURCE

It will create database connection string in the Entity Data Model Wizard. Click Next.

NEXT

From the next Entity Data Model Wizard, we can choose database items, such as Tables, Views, Stored Procedures and Function. Choose as per your requirement and pass the Model Name and click to Finish.

TABLES

The DemoDataModel.Context.cs file contains a class that derives from the DbContext class. It also provides a property for each model class that corresponds to a database table. The Department.cs and Employee.cs files contain the model classes that represent the databases tables. Here you can see Department.cs and Employee.cs represent the database the database table. They contain all columns as properties.

The following is the code for Department.cs.

  1. namespace DatabaseFirstDemo.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     public partial class Department  
  6.     {  
  7.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2214:DoNotCallOverridableMethodsInConstructors")]  
  8.         public Department()  
  9.         {  
  10.             this.Employees = new HashSet < Employee > ();  
  11.         }  
  12.         public int DepartmentId  
  13.         {  
  14.             get;  
  15.             set;  
  16.         }  
  17.         public string DepartmentName  
  18.         {  
  19.             get;  
  20.             set;  
  21.         }  
  22.         [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage""CA2227:CollectionPropertiesShouldBeReadOnly")]  
  23.         public virtual ICollection < Employee > Employees  
  24.         {  
  25.             get;  
  26.             set;  
  27.         }  
  28.     }  
  29. }  
Employee.cs
  1. namespace DatabaseFirstDemo.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.     public partial class Employee  
  6.     {  
  7.         public int Id  
  8.         {  
  9.             get;  
  10.             set;  
  11.         }  
  12.         public string Name  
  13.         {  
  14.             get;  
  15.             set;  
  16.         }  
  17.         public string Email  
  18.         {  
  19.             get;  
  20.             set;  
  21.         }  
  22.         public string Age  
  23.         {  
  24.             get;  
  25.             set;  
  26.         }  
  27.         public string Address  
  28.         {  
  29.             get;  
  30.             set;  
  31.         }  
  32.         public Nullable < int > DepartmentId  
  33.         {  
  34.             get;  
  35.             set;  
  36.         }  
  37.         public virtual Department Department  
  38.         {  
  39.             get;  
  40.             set;  
  41.         }  
  42.     }  
  43. }  
Before proceeding further you need to build the application. Next, we will generate the code from Models. Before going ahead please build the application.

In the next part, we will see how to create user interface using Scaffolding.

Thanks for reading this article, hope you enjoyed it.


Similar Articles