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.
-
43:47
Man in America
5 hours agoALERT: Putin's Plan BACKFIRES as WW3 Economic War Intensifies w/ Dr. Kirk Elliott
17.5K1 -
20:41
Exploring With Nug
1 day ago $0.43 earned2 Missing Teenagers Found 20 Years Later In The Town River!
4.11K19 -
1:46:12
Fresh and Fit
7 hours agoViva Frei Meets Fresh&Fit
47.9K21 -
54:42
LFA TV
17 hours agoThe Fascinating Story of How Donald Trump Didn’t Remember the Author of ‘Ladies Who Punch’ | Trumpet Daily 6.17.24 9pm EST
14K11 -
4:50
Guns & Gadgets 2nd Amendment News
7 hours agoWOW! Hunter Biden Now Poised To Overturn Unconstitutional Federal Gun Law
19.5K16 -
2:02:59
Robert Gouveia
6 hours agoJack Smith's "Deadly Force"; 24 States JOIN TRUMP; Biden Blames DEEP FAKES
42.7K24 -
1:35:25
Donald Trump Jr.
12 hours agoThe Biden Freeze, the Border Lies, and the Blatant Corruption, Live with Rodney Scott & Rep Ronny Jackson | TRIGGERED Ep.145
117K220 -
1:16:01
Dr. Drew
10 hours agoCharlie Kirk & Kari Lake: Securing Our Borders Means Securing Our Elections & Freedom, And Stopping The "Fentanyl Pipeline" Through Arizona – Ask Dr. Drew
60.7K65 -
1:47:44
2 MIKES LIVE
7 hours ago2 MIKES LIVE #79 DEEP DIVE MONDAY, WITH SPECIAL GUESTS MICHAEL TODD AND JEFFREY AHERN!
33.7K -
1:43:54
The Quartering
6 hours agoIsrael Starts ANOTHER War, US Expands The Draft, CNN SHOCKED By Trump Numbers & Biden Freezes
56.3K49