SlideShare a Scribd company logo
1 of 13
Download to read offline
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 1
Asalam U Alaikum!
Today I teach you the implementation of following:
 Entity frame work
 Ajax
Ok let start open your VS 2012
Entity frame work has 3 different approaches that are:
 First database approach
 First Model approach
 Code first approach
We cover Database first approach, it means we have database and we only use entity frame work, we
have one benefit of using entity frame work is that we do not write database code in Model, and this is
major reason we use entity frame work instead of Mvc but still Mvc is strong concept anyways let start
the implementation,
After opening VS 2012 make database with following fields,
Database name: Student
Table name: Info (Id, Name, Address, Age, Semester, Interest)
Here we do some little variation that is now we do not make only controller instead we make controller
for each view/action method for example we have three action method named add , delete , update so
we have three different controllers named Add controller , delete and update and so on etc.
Ok finally I teach you about Ajax that is very important for paper point of you, with the help of Ajax we
update only part of page instead of refreshing whole webpage and off course data validation.
Let introduced myself:
My name is Salman Mushtaq and I am form Lahore if you feel any problem contact me any time my
contact detail is:
Salman Mushtaq
03337465571
Mitf12a00@pucit.edu.pk
Face book: https://www.facebook.com/salman.mushtaq.39
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 2
Step 1 : Open Visual Studio 2012
Step 2: Create new project
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 3
Step 3:
Step 4:
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 4
Step 5: Wait …
Step 6:
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 5
You see in last picture the blank page with some option on left and right side will appear, first of all
create database in
Reference => App_Data => Add => New Item => Database (You will it done previously) . . .
After making database
Go to Model then
Add then
New Item then
Then
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 6
Then
Remember Data Context class name because it is very important it control the database connection,
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 7
Now
When you click on finish following classes will be add in Model
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 8
Now make controller, initially we have only one controller but it’s your assignment to add different
controllers for each view/action method.
Our Controller name is Home and the way how create controller you done previously.
We know that if we have some action method and that action method return its own view then we must
create its view, so we have Index action method so we must make view named Index. Cshtml.
Action method Index
public ActionResult Index()
{
StudentEntities cx = new StudentEntities();
var l = cx.Infoes.ToList();
return View(l);
}
View Index
@model List<Mvc5.Models.Info>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<table>
@foreach (var x in Model)
{
<tr>
<td>@x.Name</td>
<td><a href="/Home/Detail/@x.Id">Detail</a></td>
<td>|</td>
<td><a href="/Home/Delete/@x.Id">Delete</a></td>
<td>|</td>
<td><a href="/Home/Update/@x.Id">Update</a></td>
</tr>
}
</table>
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 9
</div>
</body>
</html>
Output:
Ok now we see that if you experience MVC, this code is how easy we do not interact with database and
SQL Queries this is benefit of Entity frame work.
Now we want that when we click on detail the detail of following person will show so we make another
action method Detail and its view but have link of Go back.
Action method Detail:
public ActionResult Detail(int id)
{
StudentEntities cx = new StudentEntities();
var q = cx.Infoes.Find(id);
return View(q);
}
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 10
View Detail:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Detail</title>
</head>
<body>
<div>
<table>
<tr>
<td>Name:</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Age:</td>
<td>@Model.Age</td>
</tr>
<tr>
<td>Address:</td>
<td>@Model.Address</td>
</tr>
<tr>
<td>Interest:</td>
<td>@Model.Interest</td>
</tr>
<tr>
<td><a href="/Home/Index">Go back</a></td>
</tr>
</table>
</div>
</body>
</html>
Output:
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 11
Ok if I click on Go back the navigation is performed from Detail to index, wow how work
is simple and easy now, further we develop whole application.
Ok let start now we want that if we click on Delete the record will be deleted and page
navigate to Index page with new data.
We make another action method Delete but we do not need View because it directly navigate
to Index.
Action method Delete:
public ActionResult Delete(int id)
{
StudentEntities cx = new StudentEntities();
var q = cx.Infoes.Find(id);
cx.Infoes.Remove(q);
cx.SaveChanges();
var l = cx.Infoes.ToList();
return View("Index",l);
}
Ok now we want that when we click on Update the Page is open and the updating is occur.
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 12
Now we need another Action method Update and view that show detail or fields to be
updated and a button.
Action Method Update:
public ActionResult Update(int id)
{
StudentEntities cx = new StudentEntities();
var q = cx.Infoes.Find(id);
return View(q);
}
View Update:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Update</title>
</head>
<body>
<div>
<form action="/Home/update" method="Post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="Name" value="@Model.Name" /></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="Age" value="@Model.Age" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="Address" value="@Model.Address" /></td>
</tr>
<tr>
<td>Interest</td>
<td><input type="text" name="Name" value="@Model.Interest" /></td>
</tr>
<tr>
<td><input type="submit" value="Update" /></td>
<td><a href="/Home/Index">Go back</a></td>
<td><input type="hidden" name="Id" value="@Model.Id" /></td>
</tr>
</table>
</form>
Entity frame work by Salman Mushtaq
Salman Mushtaq Page 13
</div>
</body>
</html>
Method update:
[HttpPost]
public ActionResult update()
{
int Id = int.Parse(Request["ID"]);
string Name = Request["Name"];
int Age = int.Parse(Request["Age"]);
string Address = Request["Address"];
string Interest = Request["Interest"];
StudentEntities cx = new StudentEntities();
var q = cx.Infoes.Find(Id);
q.Name = Name;
q.Age = Age;
q.Address = Address;
q.Interest = Interest;
cx.SaveChanges();
var l = cx.Infoes.ToList();
return View("Index", l);
}
Hope you understand.

More Related Content

Viewers also liked

BioSHaRE - UMCG Close out meeting 20160118
BioSHaRE - UMCG Close out meeting 20160118 BioSHaRE - UMCG Close out meeting 20160118
BioSHaRE - UMCG Close out meeting 20160118 Lisette Giepmans
 
Skin Yeast Infection Causes
Skin Yeast Infection CausesSkin Yeast Infection Causes
Skin Yeast Infection CausesEvie Dawson
 
Personal hygiene-of-both-male-and-female
Personal hygiene-of-both-male-and-female Personal hygiene-of-both-male-and-female
Personal hygiene-of-both-male-and-female Assyla Neolag
 
인터랙션 디자인 1310585 최하늘
인터랙션 디자인  1310585 최하늘인터랙션 디자인  1310585 최하늘
인터랙션 디자인 1310585 최하늘하늘 최
 
Comparative metagenomics: quantifying similarities between environments, CMBI...
Comparative metagenomics: quantifying similarities between environments, CMBI...Comparative metagenomics: quantifying similarities between environments, CMBI...
Comparative metagenomics: quantifying similarities between environments, CMBI...Copenhagenomics
 
Sequencing the transcriptome reveals complex layers of regulation, Department...
Sequencing the transcriptome reveals complex layers of regulation, Department...Sequencing the transcriptome reveals complex layers of regulation, Department...
Sequencing the transcriptome reveals complex layers of regulation, Department...Copenhagenomics
 

Viewers also liked (7)

BioSHaRE - UMCG Close out meeting 20160118
BioSHaRE - UMCG Close out meeting 20160118 BioSHaRE - UMCG Close out meeting 20160118
BioSHaRE - UMCG Close out meeting 20160118
 
Report 1
Report 1Report 1
Report 1
 
Skin Yeast Infection Causes
Skin Yeast Infection CausesSkin Yeast Infection Causes
Skin Yeast Infection Causes
 
Personal hygiene-of-both-male-and-female
Personal hygiene-of-both-male-and-female Personal hygiene-of-both-male-and-female
Personal hygiene-of-both-male-and-female
 
인터랙션 디자인 1310585 최하늘
인터랙션 디자인  1310585 최하늘인터랙션 디자인  1310585 최하늘
인터랙션 디자인 1310585 최하늘
 
Comparative metagenomics: quantifying similarities between environments, CMBI...
Comparative metagenomics: quantifying similarities between environments, CMBI...Comparative metagenomics: quantifying similarities between environments, CMBI...
Comparative metagenomics: quantifying similarities between environments, CMBI...
 
Sequencing the transcriptome reveals complex layers of regulation, Department...
Sequencing the transcriptome reveals complex layers of regulation, Department...Sequencing the transcriptome reveals complex layers of regulation, Department...
Sequencing the transcriptome reveals complex layers of regulation, Department...
 

Similar to Entity Framework Database First Approach

Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletMitchinson
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenSony Suci
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaalifha12
 
Previous weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docxPrevious weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docxkeilenettie
 
Cis407 a ilab 4 web application development devry university
Cis407 a ilab 4 web application development devry universityCis407 a ilab 4 web application development devry university
Cis407 a ilab 4 web application development devry universitylhkslkdh89009
 
Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7helpido9
 
ASP.NET MVC 5 Building Your First Web Application (A Beginner S Guide
ASP.NET MVC 5  Building Your First Web Application (A Beginner S GuideASP.NET MVC 5  Building Your First Web Application (A Beginner S Guide
ASP.NET MVC 5 Building Your First Web Application (A Beginner S GuideAlicia Buske
 
Aspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAlfa Gama Omega
 
Adding a view
Adding a viewAdding a view
Adding a viewNhan Do
 
ASP.net Manual final.pdf
ASP.net Manual final.pdfASP.net Manual final.pdf
ASP.net Manual final.pdfSwapnilGujar13
 
Cis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universityCis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universitylhkslkdh89009
 
asp.net - for merge.docx
asp.net - for merge.docxasp.net - for merge.docx
asp.net - for merge.docxSwapnilGujar13
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLAkhil Mittal
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSAkshay Mathur
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010vchircu
 

Similar to Entity Framework Database First Approach (20)

Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) kerenTutorial mvc (pelajari ini jika ingin tahu mvc) keren
Tutorial mvc (pelajari ini jika ingin tahu mvc) keren
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
 
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
 
Previous weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docxPrevious weeks work has been uploaded as well as any other pieces ne.docx
Previous weeks work has been uploaded as well as any other pieces ne.docx
 
Cis407 a ilab 4 web application development devry university
Cis407 a ilab 4 web application development devry universityCis407 a ilab 4 web application development devry university
Cis407 a ilab 4 web application development devry university
 
Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7Cis 407 i lab 4 of 7
Cis 407 i lab 4 of 7
 
ASP.NET MVC 5 Building Your First Web Application (A Beginner S Guide
ASP.NET MVC 5  Building Your First Web Application (A Beginner S GuideASP.NET MVC 5  Building Your First Web Application (A Beginner S Guide
ASP.NET MVC 5 Building Your First Web Application (A Beginner S Guide
 
Aspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_csAspnet mvc tutorial_01_cs
Aspnet mvc tutorial_01_cs
 
Adding a view
Adding a viewAdding a view
Adding a view
 
Mvc acchitecture
Mvc acchitectureMvc acchitecture
Mvc acchitecture
 
ASP.net Manual final.pdf
ASP.net Manual final.pdfASP.net Manual final.pdf
ASP.net Manual final.pdf
 
Cis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universityCis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry university
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
asp.net - for merge.docx
asp.net - for merge.docxasp.net - for merge.docx
asp.net - for merge.docx
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Creating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JSCreating Single Page Web App using Backbone JS
Creating Single Page Web App using Backbone JS
 
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
 

More from Salman Mushtaq

More from Salman Mushtaq (7)

Bootstrap 3 Lecture 5
Bootstrap 3 Lecture 5Bootstrap 3 Lecture 5
Bootstrap 3 Lecture 5
 
LINQ to SQL
LINQ to SQLLINQ to SQL
LINQ to SQL
 
Captcha
CaptchaCaptcha
Captcha
 
ADO DOT NET
ADO DOT NETADO DOT NET
ADO DOT NET
 
Contact Book Version 1.0
Contact Book Version 1.0Contact Book Version 1.0
Contact Book Version 1.0
 
Database By Salman Mushtaq
Database By Salman MushtaqDatabase By Salman Mushtaq
Database By Salman Mushtaq
 
Serialization
SerializationSerialization
Serialization
 

Entity Framework Database First Approach

  • 1. Entity frame work by Salman Mushtaq Salman Mushtaq Page 1 Asalam U Alaikum! Today I teach you the implementation of following:  Entity frame work  Ajax Ok let start open your VS 2012 Entity frame work has 3 different approaches that are:  First database approach  First Model approach  Code first approach We cover Database first approach, it means we have database and we only use entity frame work, we have one benefit of using entity frame work is that we do not write database code in Model, and this is major reason we use entity frame work instead of Mvc but still Mvc is strong concept anyways let start the implementation, After opening VS 2012 make database with following fields, Database name: Student Table name: Info (Id, Name, Address, Age, Semester, Interest) Here we do some little variation that is now we do not make only controller instead we make controller for each view/action method for example we have three action method named add , delete , update so we have three different controllers named Add controller , delete and update and so on etc. Ok finally I teach you about Ajax that is very important for paper point of you, with the help of Ajax we update only part of page instead of refreshing whole webpage and off course data validation. Let introduced myself: My name is Salman Mushtaq and I am form Lahore if you feel any problem contact me any time my contact detail is: Salman Mushtaq 03337465571 Mitf12a00@pucit.edu.pk Face book: https://www.facebook.com/salman.mushtaq.39
  • 2. Entity frame work by Salman Mushtaq Salman Mushtaq Page 2 Step 1 : Open Visual Studio 2012 Step 2: Create new project
  • 3. Entity frame work by Salman Mushtaq Salman Mushtaq Page 3 Step 3: Step 4:
  • 4. Entity frame work by Salman Mushtaq Salman Mushtaq Page 4 Step 5: Wait … Step 6:
  • 5. Entity frame work by Salman Mushtaq Salman Mushtaq Page 5 You see in last picture the blank page with some option on left and right side will appear, first of all create database in Reference => App_Data => Add => New Item => Database (You will it done previously) . . . After making database Go to Model then Add then New Item then Then
  • 6. Entity frame work by Salman Mushtaq Salman Mushtaq Page 6 Then Remember Data Context class name because it is very important it control the database connection,
  • 7. Entity frame work by Salman Mushtaq Salman Mushtaq Page 7 Now When you click on finish following classes will be add in Model
  • 8. Entity frame work by Salman Mushtaq Salman Mushtaq Page 8 Now make controller, initially we have only one controller but it’s your assignment to add different controllers for each view/action method. Our Controller name is Home and the way how create controller you done previously. We know that if we have some action method and that action method return its own view then we must create its view, so we have Index action method so we must make view named Index. Cshtml. Action method Index public ActionResult Index() { StudentEntities cx = new StudentEntities(); var l = cx.Infoes.ToList(); return View(l); } View Index @model List<Mvc5.Models.Info> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <table> @foreach (var x in Model) { <tr> <td>@x.Name</td> <td><a href="/Home/Detail/@x.Id">Detail</a></td> <td>|</td> <td><a href="/Home/Delete/@x.Id">Delete</a></td> <td>|</td> <td><a href="/Home/Update/@x.Id">Update</a></td> </tr> } </table>
  • 9. Entity frame work by Salman Mushtaq Salman Mushtaq Page 9 </div> </body> </html> Output: Ok now we see that if you experience MVC, this code is how easy we do not interact with database and SQL Queries this is benefit of Entity frame work. Now we want that when we click on detail the detail of following person will show so we make another action method Detail and its view but have link of Go back. Action method Detail: public ActionResult Detail(int id) { StudentEntities cx = new StudentEntities(); var q = cx.Infoes.Find(id); return View(q); }
  • 10. Entity frame work by Salman Mushtaq Salman Mushtaq Page 10 View Detail: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Detail</title> </head> <body> <div> <table> <tr> <td>Name:</td> <td>@Model.Name</td> </tr> <tr> <td>Age:</td> <td>@Model.Age</td> </tr> <tr> <td>Address:</td> <td>@Model.Address</td> </tr> <tr> <td>Interest:</td> <td>@Model.Interest</td> </tr> <tr> <td><a href="/Home/Index">Go back</a></td> </tr> </table> </div> </body> </html> Output:
  • 11. Entity frame work by Salman Mushtaq Salman Mushtaq Page 11 Ok if I click on Go back the navigation is performed from Detail to index, wow how work is simple and easy now, further we develop whole application. Ok let start now we want that if we click on Delete the record will be deleted and page navigate to Index page with new data. We make another action method Delete but we do not need View because it directly navigate to Index. Action method Delete: public ActionResult Delete(int id) { StudentEntities cx = new StudentEntities(); var q = cx.Infoes.Find(id); cx.Infoes.Remove(q); cx.SaveChanges(); var l = cx.Infoes.ToList(); return View("Index",l); } Ok now we want that when we click on Update the Page is open and the updating is occur.
  • 12. Entity frame work by Salman Mushtaq Salman Mushtaq Page 12 Now we need another Action method Update and view that show detail or fields to be updated and a button. Action Method Update: public ActionResult Update(int id) { StudentEntities cx = new StudentEntities(); var q = cx.Infoes.Find(id); return View(q); } View Update: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Update</title> </head> <body> <div> <form action="/Home/update" method="Post"> <table> <tr> <td>Name:</td> <td><input type="text" name="Name" value="@Model.Name" /></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="Age" value="@Model.Age" /></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="Address" value="@Model.Address" /></td> </tr> <tr> <td>Interest</td> <td><input type="text" name="Name" value="@Model.Interest" /></td> </tr> <tr> <td><input type="submit" value="Update" /></td> <td><a href="/Home/Index">Go back</a></td> <td><input type="hidden" name="Id" value="@Model.Id" /></td> </tr> </table> </form>
  • 13. Entity frame work by Salman Mushtaq Salman Mushtaq Page 13 </div> </body> </html> Method update: [HttpPost] public ActionResult update() { int Id = int.Parse(Request["ID"]); string Name = Request["Name"]; int Age = int.Parse(Request["Age"]); string Address = Request["Address"]; string Interest = Request["Interest"]; StudentEntities cx = new StudentEntities(); var q = cx.Infoes.Find(Id); q.Name = Name; q.Age = Age; q.Address = Address; q.Interest = Interest; cx.SaveChanges(); var l = cx.Infoes.ToList(); return View("Index", l); } Hope you understand.