Premium Only Content
Part 8 Data access in mvc using entity framework
Tags
asp.net mvc database tutorial
asp.net mvc database application tutorial
creating asp.net mvc application with database
asp.net mvc database connection using entity framework
asp.net mvc database project
database connectivity in asp.net mvc
The controller responds to URL request, gets data from a model and hands it over to the view. The view then renders the data. Model can be entities or business objects.
In part 7, we have built Employee entity.
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
In this video, we will discuss, retrieving data from a database table tblEmployee using entity framework. In a later video, we will discuss using business objects as our model.
Step 1: Install entity framework, if you don't have it installed already on your computer. At the time of this recording the latest version is 5.0.0.0. Using nuget package manager, is the easiest way to install. A reference to EntityFramework.dll is automatically added.
Open visual studio - Tools - Library Package Manager - Manage NuGet Packages for Solution
Step 2: Add EmployeeContext.cs class file to the Models folder. Add the following "using" declaration.
using System.Data.Entity;
Copy & paste the following code in EmployeeContext.cs
public class EmployeeContext : DbContext
{
// Replace square brackets, with angular brackets
public DbSet[Employee] Employees {get; set;}
}
EmployeeContext class derives from DbContext class, and is responsible for establishing a connection to the database. So the next step, is to include connection string in web.config file.
Step 3: Add a connection string, to the web.config file, in the root directory.
Step 4: Map "Employee" model class to the database table, tblEmployee using "Table" attribute as shown below.
[Table("tblEmployee")]
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
Note: "Table" attribute is present in "System.ComponentModel.DataAnnotations.Schema" namespace.
Step 5: Make the changes to "Details()" action method in "EmployeeController" as shown below.
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(x =] x.EmployeeId == id);
return View(employee);
}
Step 6: Finally, copy and paste the following code in Application_Start() function, in Global.asax file. Database class is present "in System.Data.Entity" namespace. Existing databases do not need, database initializer so it can be turned off.
Database.SetInitializer[MVCDemo.Models.EmployeeContext](null);
That's it, run the application and notice that the relevant employee details are displayed as expected.
-
LIVE
Dr Disrespect
3 hours ago🔴LIVE - DR DISRESPECT - MARVEL RIVALS - GOLD VANGUARD
4,796 watching -
LIVE
The Quartering
3 hours agoTrump To INVADE Mexico, Take Back Panama Canal Too! NYC Human Torch & Matt Gaetz Report Drops!
4,010 watching -
LIVE
Nerdrotic
3 hours agoA Very Merry Christmas | FNT Square Up - Nerdrotic Nooner 453
1,232 watching -
1:14:05
Tucker Carlson
3 hours ago“I’ll Win With or Without You,” Teamsters Union President Reveals Kamala Harris’s Famous Last Words
29.1K137 -
1:58:31
The Dilley Show
3 hours agoTrump Conquering Western Hemisphere? w/Author Brenden Dilley 12/23/2024
40.9K7 -
1:09:59
Geeks + Gamers
4 hours agoSonic 3 DESTROYS Mufasa And Disney, Naughty Dog Actress SLAMS Gamers Over Intergalactic
18.8K5 -
51:59
The Dan Bongino Show
5 hours agoDemocrat Donor Admits The Scary Truth (Ep. 2393) - 12/23/2024
496K1.46K -
2:32:15
Matt Kohrs
15 hours agoRumble CEO Chris Pavlovski Talks $775M Tether Partnership || The MK Show
83.5K26 -
28:23
Dave Portnoy
15 hours agoDavey Day Trader Presented by Kraken - December 23, 2024
98K32 -
59:29
BonginoReport
6 hours agoTrump, Murder Plots, and the Christmas Miracle: Evita + Jack Posobiec (Ep.110) - 12/23/2024
111K96