Did you like how we did? Rate your experience!

Rated 4.5 out of 5 stars by our customers 561

Video instructions and help with filling out and completing What Form 2220 Deduction

Instructions and Help about What Form 2220 Deduction

Since part three of entity framework tutorial in this video we'll discuss the code first approach of entity framework entity framework supports all these three approaches schema first model first and code first in part one we discuss the schema first approach model first approach is discussed in part two and in this video we'll discuss the code first approach code first approach allows us to create our custom classes first and based on those custom classes entity framework can automatically generate the database and create the required tables for us let's understand using the code first approach with an example we'll be modifying the same example that we worked with in the previous session of this video series so let's flip to visual studio so notice that we already have this employee model dot e DMX file which we have generated using the model first approach in the previous session of this video series so let's go and delete that first because we no longer require that and let's also delete this employee model dot e dmx dot SQL file which contains the required sequel to create the database tables so let's go ahead and delete that as well alright all we have at the moment is the web config file and the webform which contains the grid view control and entity data source control so to this project let's add a class file and let's call this department dot CS so this file is going to contain the definition for Department class so what other things that a typical department contains ID name location and a department will also contain you know some employees so it is also going to contain maybe employees property which is going to return list of employees back okay so basically what department class is going to look like this with ID name location and employees property which is going to return list of employee objects okay to speed things up I have already typed these properties so let's copy them from this notepad and paste within department class okay and if you notice at the moment look at this employee we have a compilation error why is that that's because we don't have the employee class at so let's go ahead and create the employee class so again to this project let's add another class file and let's call it employee dot CS and what are the property is that an employee you know should have that is ID first name last name gender salary and the department to which the employee belong so the employee class will look something like this and to speed things up again I have all these properties type so let's copy them from the notepad and then paste within our employee class okay now this department property within the employee class and employees property within the department class or navigation properties the rest of them are scalar properties okay so department dot employees you know we are trying to retrieve you know objects of another type so basically list of employees they belong to another table so it's a navigation property in entity framework terms similarly an employee object department is a navigation property okay so we have the custom classes that are required for every application because basically within our web form we want to display the department ID name location and the list of employees you know who belong to that specific department okay so basically to achieve this functionality we are first creating our custom classes with the required properties and based on these classes we are now going to generate the underlying database tables let's see how to achieve that with the help of entity framework so the next step here is to add another class file so so far what we have done we have just created our custom classes the department and employee classes so step three is to add another class file and let's call this employee dbcontext you can give this file any name you want but the reason why I have chosen this name you know there's a specific reason for that will discuss that in just a bit so we are going to make this class and flow dbcontext inherit from another class called dbcontext and this class is present in systems or data entity namespace which is present within this entity framework assembly so let's go ahead and bring in system dot data entity namespace and then let's make this class employee dbcontext inherit from DB context class so this is the class it does you know base class DB context is what is doing you know all the work to interact with the database retrieve data for us and then then populate our custom objects okay so we are going to inherit from that class and within this employee DB context class I'm going to have two properties okay and both of them are going to be public and what are these property is going to return these properties are going to return a DB set of Dept objects okay I am going to call the property departments and then let's have a gap and a set accessor similarly let's have another public property and this one is going to return List I mean DB set of employee objects and let's call the property employees so very simple class employee dbcontext with just two public properties okay now there is another thing that we need to do basically you know let's add another class file and again we can give this class file any meaningful name we want but I'm going to call it employee repository so basically this class is going to contain you know our methods which are going to return the data that we want okay so here what we want you know you can write methods to return list of employees list of departments or one employee one department.

If you believe that this page should be taken down, please follow our DMCA take down process here.