Part 8 Data access in mvc using entity framework

preview_player
Показать описание
Tags

Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET MVC Text Articles

All ASP .NET MVC Slides

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

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.

Open visual studio - Tools - Library Package Manager - Manage NuGet Packages for Solution

using System.Data.Entity;

public class EmployeeContext : DbContext
{
// Replace square brackets, with angular brackets
public DbSet[Employee] Employees {get; set;}
}

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);
}

Database.SetInitializer[MVCDemo.Models.EmployeeContext](null);

That's it, run the application and notice that the relevant employee details are displayed as expected.
Рекомендации по теме
Комментарии
Автор

I am new to MVC and my company gave me one project based on MVC with a description do or die, so i didn't find any solutions from last few days. But after watching some of these videos i am now clear that how to start that project. Really HATS OFF to you VENKAT SIR..

sheshendraguntamukkala
Автор

We will discuss about editing, deleting, updating data in our upcoming videos.

Csharp-video-tutorialsBlogspot
Автор

I was following someone else's tutorials to get this done, I've spent about 5 hours trying to wrap my head around it, then I came here and in 15 minutes I got it done! Awesome tutorials, thank you <3

vladvlad
Автор

Hi Rob, very sorry for not replying to your question in time. The problem is, I am getting too many questions and not finding time to reply to everyone on time. Hope you can help me, by answering some of the questions, for which you have answers.

Coming to your question. Try this
1. Expand "References" folder from "Solution Explorer"
2. Delete the reference for
Will be

Csharp-video-tutorialsBlogspot
Автор

Can you make sure that you have an object in "homepages" collection property where Id is 1. You can very easily check this by debugging your application. Also, please make sure to check the underlying table has a rows where Id = 1. Hope this helps. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists. Please share the link with your friends.

Csharp-video-tutorialsBlogspot
Автор

At 5:23 of the presentation, did you mean to say that you should manually type in a Connection String, similar to that shown at 3:27, in notepad?

Because, unless I overlooked something, I did not get an automatic creation of that connection string when I created my EmployeeContext.cs file.

I'm running VS 2013 Express.

Otherwise, I find these tutorials very helpful.. Thanks very much!

evanflink
Автор

Another great video. What struck me was that you were able to interrogate the database with writing any SQL!

Presumably you can use the approach shown in this video to interrogate much more complex queries (or views) in SQL Server provided those queries have already been written in SQL Server first. I'm going to try this out.

kaligasm
Автор

Thank you for sharing your valuable knowledge with us ! Very simple and nice video to understand ! Thanks a lot !

DineshKudale
Автор

Your tutorials are some of the best I have seen. Thank you.

rosw
Автор

you change my attitude towards .net technology are awesome....i have realy respect for you becoz you share a good video and understanble thinks donot stop to upload new videos ....thanks

abhivaxs
Автор

Your tutorial full of details and clear, really being a great helper . Thanks U.

emmazh
Автор

Hi Guys people who are not able to move ahead with the database connection.
Please note that when you install Entity Framework now from Nuget. It Downloads the Newest version which is EF 6.0.2. You need to do additional configuration changes to make it work, So i would prefer to suggest install from Nuget by Nuget Package manager condole and download EF 5.0 which is supported by this tuotrial.

Steps:Go to Tools -> Library Package Manager->Package Manager Console and Type PM> Install-Package EntityFramework -Version 5.0.0

Hope this helps you all :)

ImranAnsariM
Автор

Yes, it should as long as the same schema and metadata exists on the full-fledged SQL Server that you are switching over to. Thank you very much for taking time to give feedback. I am very glad you found these videos useful.

Csharp-video-tutorialsBlogspot
Автор

Excellent video series!! Thank you very much for posting. If you are getting errors like "The underlying provider failed on Open", check the error's details to find the inner exception. There can be more info to pinpoint what is wrong. You may have login issues or problems in your connection string in the config file. Check your database service to see if a password is enabled. If so you need to provide it in the config string or remove the password in the service.

kenmtb
Автор

Thank you venkat,
I have complete part 8 without any error.... you are great!!!

SydurRahman
Автор

Folks who ever have a trouble creating a connection string with the DB in SQL Server Express, follow these steps:

1. First create a DB named Sample in SQL Server express
2. Add the necessary table named tblTable and data into it
3. Go to web.config file inside your visual studio MVC demo project
4. Open a connection string tag, and paste the below code into it:

<add name="EmployeeContext" connectionString="server=Your server instance name; database=Sample; integrated security=SSPI"

5. Refresh the server explorer.
6. Your DB is connected.!!

Thank you

kartheeknagulapati
Автор

Hail from Brazil, thanks for the class.

brenosantin
Автор

Thanks for this tutorials man! God bless you!

bsStAB
Автор

Dear Sir,

Excellent explanation .Really like your video as it is very easy to understand. You explain very well.

vancha

vanchalakhotia
Автор

sirji u r great u made Dotnet a very easy language you are sachin tendulkar in the field of programming
thank you so much sir

anuragpandey
join shbcf.ru