Part 1 What is Entity Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
to present technologies I invented this is part one of entity framework tutorial in this video we'll discuss what is entity framework so what is entity framework entity framework is an ORM framework or M stands for object relational mapping so it's object relational mapping framework then object relational mapping framework automatically creates classes based on database tables and the YC versa is also true that is it can also automatically generate necessary sequel to create database tables based on classes let's understand what entity framework can provide with an example let's assume we have caught these two tables the first table is the department's table which has got ID name and location columns the second table is the employees table which is got ID first name last name gender salary and department ID columns now this department ID is the foreign key column so we look up this value within the department's table now let's say based on these two tables we want to implement in asp.net web form that would display departments and employees data as you can see here notice that the first row is displaying IT department and its employees the second and third rows we have HR and payroll departments and their respective employees now to implement an asp.net web form like this what are the typical steps that are involved we first need to create department and employee custom classes then we need to write ad or dotnet code to retrieve data from the respective underlying database tables once we have the data retrieved we then need to create instances of department and employee classes and then we need to populate those instances with the data that we have retrieved so these steps are kind of repetitive every time we add a new table to our application so every time we add a new table we first need to create a custom class so typically the name of the class will be the name of the table and then the properties of the class will be the columns within the database table and once we have that custom class created we need to write ad or dotnet code to go and fetch data to update insert and delete data and then we need to create an instance of that custom class and then populate that object with the data that we have retrieved now entity framework can do all of that for us automatically let's understand that with an example so first we need to install entity framework itself and the easiest way to install entity framework is by using new bit package manager so let's actually flip to visual studio so at the moment what I have done is created in new asp.net MT performs application and to install you know entity framework we first need to have new get package manager installed - if you don't have new grid package manager installed go to tools and then go to extension manager and then search for new get and then you will have new get package manager within the list go ahead and install that and make sure you have online gallery selected when you are searching for new get package manager ok so once you have nougat package manager installed then go ahead and click on tools again go to library package manager and then you should see manage new get packages for solution option so open this option and then click on online tab and then search for entity framework at the moment I already have entity framework installed now as of this recording the latest version is 6.1 ok so I have this entity framework installed so as soon as you install entity framework what's going to happen it's also going to add a reference to the entity framework assembly within the solution as you can see here okay so entity framework is already installed on my machine and now we also need to create the two database tables that is the departments and employees database tables and in the interest of time I have already implemented the sequel script so here we are creating the departments table and this script will create the employee's table and these two pieces of scripts here this is going to populate the department's table with some sample data and this script is going to populate the employees table with some sample data all right and I have already executed the script so within our sample database I already have departments and employees tables so when I execute the query they have the data from both the tables now if we provide the entity framework with the schema with this database schema entity framework is automatically going to create the respective classes for us that is the department and employee classes it is also going to write the required area dotnet code to retrieve data from the underlying database tables and then populate these objects with the data that is retrieved okay let's actually look at that in action so let's flip to visual studio so the first step here is to add an area dot identity data model so let's right click on the project and add a new item and we want to add a Teodoro entity data model the easiest way to find the template is to select data tab here and then select ato dot and entity data model and let's call our model employee model and then click Add so we want to generate you know our entities from the database so select that option and then click Next and then you know here it's asking you you know to specify a connection now click on the new connection button if you don't already see a connection and then specify the name of the server in my case sequel server is running on this local machine so I'm going to use dot ok and then I'm going to use Windows authentication and the database in our case is sample so I'm going to select that database and we can test the connection so test connection succeeded click ok so once you have that so it's asking us or you know to save the connection settings in the web config file now here you can give it any name ok now I'm going to call this employee dbcontext ok and there is a specific reason why I'm naming it like that employee dbcontext but we'll discuss that maybe in a later video session but for now you know I've just changed it to employee dbcontext and what's going to happen when I click Next you know whatever server information that we have specified you know it's going to save that connection string with that name within the web config file and behind the scenes it's also going to generate another class called employee dbcontext we'll discuss that in detail in a later video session for now let's just understand that with this name there will be a connection string within the web.com fit file so let's click Next now now it's going to connect the database and then it's going to retrieve the tables views and stored procedures and then we need to select the two tables based on which we want to generate entities in our case they are departments and employees and then let's change the model namespace to employee model now look at this two checkboxes here PluralEyes or singular singulars generated object names include foreign key columns in the model so obviously these are self-explanatory now look at this the table names are departments and employees I know basically because you know at department's table will contain several departments so it makes sense to have the name as departments similarly employees table will have several employees but then when you look at a department class it's going to contain just one department so it's going to singular eyes the entity name the class name that it's going to generate okay it's a very good option and then it's also going to include foreign key columns in the model okay so leave those two default options there and then click finish so now this is going to generate the entity model for us automatically ok so notice that it has added this file employee model and then we have department and employee classes ok and look at the properties ID name location these correspond to the columns within the underlying database table and notice that we have navigation properties here so a department can contain multiple employees so we have employees property there so when we use this property we are going to get all the employees belonging to that department and similarly an employee belong to a specific department ok so there is a department navigation property along with you know all the other properties so basically this will correspond to the columns in the underlying database table ok so a department can have many employees so one-to-many relationship there all right and if you look at this employee model dot designer dot CS file you should actually have the entities that I am talking about so basically notice that we have department entity and then you should also have the properties so we have the ID property we have the name property and you should see other properties as well similarly we should also have employee entity and then their respective properties so here we have the employee property class and the respective properties of the employee class now let's go ahead and add a webform to a project so at this point we have everything you know we have the employee class created departments class created the respective properties are there it has also written the required 80 ordinate qualitative data from the underlying database tables and then populate these objects with the required data now let's go ahead and add a webform and let's call it webform one dot aspx and first of all here let's set the style attribute let's set font family to area and let's drag and drop a grid view control the grid we control should be under the data tab and then let's also drag and drop entity data source control okay so let's flip to the design mode so we have the grid view control entity datasource controls let's Kahn auto format this grid view control let's actually choose this colorful skin to make it look a little nice and then let's configure the entity data source so configure the data source so named connection now if I go ahead and drop this down look at this employee dbcontext now if you recollect this is the name that we have used for the connection string that is stored in the web config file now when I select this look at this it says you know unable to load the specified metadata resource that's basically because we have not compiled our solution at so let's go ahead and compile this solution now let's go ahead and configure the data source so let's select this employee dbcontext click Next and then what is the entity set that we want we are going to select departments for now remember this is how we want the data to be displayed so we want the department ID name location and then the employees that belong to that department so here I'm going to select departments which is going to have ID name location and if you recollect you know the employee data model keep in mind it also had a navigation property called employees okay so we will be able to get employees belonging to this department as well okay well we'll look at that in just a bit so we want to select all these properties of that departments entity and then click finish so we have our entity data source configured now let's associate this entity data source with this grid we control and to do that I am going to select this entity data source there so we have ID name and location now we also want to display employees belonging to each department so what I'm going to do here is actually a edit columns and then I'm going to add another column let's actually add template field now if you're new to grid view control we have a video series explaining extensively about the grid view control and how to use various fields that we have so please watch the videos from grid view video series okay so let's add template field and then let's specify the heading for template feels as for this template field less employees let's go ahead and click OK and now we want to edit the templates of this template field so I'm going to click edit templates and then I'm going to drag and drop another grid we control within the item template of that template field that we have just added okay because that's what is the grid we control that we are going to use to display employees details so let's go ahead drag and drop another grid we control here and then we need to edit data bindings for the screed view control so what do we want to display within this grid we control the employees information so I'm going to click on the smart tags are smart tasks button and then select edit data bindings then I'm going to choose this custom binding and then we are going to use this eval method so we want to display employees details and keep in mind there is a navigation property called employees on the department object okay so it's going to pull the employees and then display within this grid view control so let's click OK and let's go back to the main grid view control and then end a template editing okay with all those changes let's go ahead and run our waveform so now we expect it to display department ID name location and the respect to employees let's see if that's what we get ok now look at this we got ID name location that we didn't get employees details why is that that's basically because we need to do one more little thing here so if we go to the entity datasource and if we get to the properties of that entity datasource now by default what is this entity datasource going to do it's going to retrieve only the department details it's not going to load the employees details so we want to tell it to load the employees details as well so this property called include where we can specify you know the employees navigation property so this is going to load employees details as well so with that change let's save it let's go and run it once more and notice that we have department ID name location and the employees but what is our requirement we don't want to display you know the employee ID and department ID columns if that's the case what we need to do is we need to configure the grid view control within the template field so basically go to edit templates and within the template will we have the grid view control so let's go to edit columns now it's automatically generating columns for us now we don't want that to happen we want to control what columns we want to display we only want to display first-name lastname gender and salary columns so what we're going to do here is add in our bound columns so this bound column here the header text is going to be first name and the data field is going to be first-name let's add another add bone column add a text last name the data field is going to be lastname let's quickly do that let's add another pound field header text is going to be gender data field is also going to be gender and then let's add another bone fill finally we want to add salary so salaries the header text and data field is salary so let's click OK and look at this it's also automatically generating the columns we don't want that to happen so get to the properties of the grid view control and then there should be a property called Auto generate columns and which is on by default let's turn it off so that should get rid of that let's end a template editing let's go and run this now we only get the columns that we expect okay all right so you first need to install the nougat package manager and then once you have nougat package manager installed using nougat package manager install entity framework and once you have installed entity framework add a do dot and entity data model and we have seen that in action alright so in this video we have used the schema first approach of the entity framework but with entity framework we can also use model first a code first approaches we'll discuss those in our subsequent videos that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 839,096
Rating: undefined out of 5
Keywords: entity framework, tutorial, 6.0, 4.0, 5.0, code first, database first, model first, asp.net, end to end
Id: Z7713GBhi4k
Channel Id: undefined
Length: 18min 1sec (1081 seconds)
Published: Sun May 04 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.