Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Model First Approach in MVC Using Entity Framework

Article:
Viewed:  1167 
Posted On:  4/17/2015 9:11:02 PM 
How we can use Model First Approach in MVC Using Entity Framework ? 

In this article I am going to explain Model First Approach in MVC using Entity Framework.

First question comes in our mind what is Entity Framework?

The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. 

Below Image will explain Entity Framework architecture.

1.png

Image 1.

What is O/RM?                          

ORM is a tool for storing data from domain objects to relational database like MS SQL Server, in an automated way, without much programming. O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information on how domain objects map to relational database objects (tables, views & Stored Procedures). ORM allows us to keep our database design separate from our domain class design.

Now we will learn Model First Approach Step By Step.

Model first is the domain modelling approach in Entity Framework. It enables you to create model's Entities, relationships, and inheritance hierarchies on the design surface of empty model (.edmx file) by using entity designer and then create database from it. 

Now Open Visual Studio -> File -> New -> Project.

2.png

Image 2.

3.png

Image 3.

Now Right Click On Model Folder -> Add New ADO.NET Entity Data Model.

4.png

Image 4.

5.png

Image 5.

Select Here Empty Model.

6.png

Image 6.

Now Right Click -> Add-> Entity.

7.png

Image 7.

Give Name, Select Data Type & Set Key.

8.png

Image 8.

Click ok.

9.png

Image 9.

Now Add More Column to your Entity.

10.png

Image 10.

It Will Add New Property or Column to your entity. Now you can set Data type and other properties of this newly added column like below.

11.png

Image 11.

Now Add all column (Properties) which you want. After this our ADO.NET Entity Data Model Will look like.

12.png

Image 12.

Now time to generate Data Base from This Model. So Right click in your edmx like below.

13.png

Image 13.

14.png

Image 14.

This will give below Message -> Click Yes.

15.png

Image 15.

This will show connection string in next step like below.

16.png

Image 16.

Click Next -> This will generate DB Script.

17.png

Image 17.

18.png

Image 18.

19.png

Image 19.

After Command executed successfully check your Data Base.

20.png

Image 20.

Now generate class. So Right Click on Models-> School.edmx ->School.tt ->RunCustomTool

21.png

Image 21.

22.png

Image 22.

Now time to perform CRUD operations. So right click on Controller -> Add -> Controller.

23.png

Image 23.

It will create CRUD Views also like below.

24.png

Image 24.

Now Run Your Application.

Add New Student.

25.png

Image 25.

Showing All Students Records.

26.png

Image 26.

Edit any student record.

27.png

Image 27.

Showing Details of Student.

28.png

Image 28.

Delete any student Record.

29.png

Image 29.

Now see your Data Base.

30.png

Image 30.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us