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.
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.
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.
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.
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.
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.
In the Entity Data Model Wizard, select EF Designer from database and click Next.
Click the New Connection button where you will define the database connection.
In the Choose Data Source window, we need to choose Data source and click Continue.
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.
It will create database connection string in the Entity Data Model Wizard. Click 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.
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.
- namespace DatabaseFirstDemo.Models
- {
- using System;
- using System.Collections.Generic;
- public partial class Department
- {
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
- public Department()
- {
- this.Employees = new HashSet < Employee > ();
- }
- public int DepartmentId
- {
- get;
- set;
- }
- public string DepartmentName
- {
- get;
- set;
- }
- [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
- public virtual ICollection < Employee > Employees
- {
- get;
- set;
- }
- }
- }
Employee.cs - namespace DatabaseFirstDemo.Models
- {
- using System;
- using System.Collections.Generic;
- public partial class Employee
- {
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public string Email
- {
- get;
- set;
- }
- public string Age
- {
- get;
- set;
- }
- public string Address
- {
- get;
- set;
- }
- public Nullable < int > DepartmentId
- {
- get;
- set;
- }
- public virtual Department Department
- {
- get;
- set;
- }
- }
- }
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.