Entity Framework Code First Approach | ADO.NET Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to nourish technologies this is sadaqa sharma we are continuing our session of a do dotnet entity framework in our previous sessions of a do dotnet entity framework we discussed about the basic architecture of a do dotnet entity framework oh what are the major components of entity framework and we discussed about the area dotnet entity framework library means what are all the basic classes required to handle database manipulations by using ad or dotnet entity framework we discussed about the classes like entity classes and the component model data annotations which are used for entity framework and what is the key concept about ad or dotnet entity framework we discuss it entity framework introduces multiple modeling techniques like code first approach database first approach and model first approach today in this session we are going to deal with code first approach so we will see how to handle a do dotnet entity framework code first approach in asp.net MVC so we will create an asp.net MVC application with code first approach the code first approach is very simple here we define all the domain-specific classes and according to the domain-specific classes we call them as poco classes entity framework will create the database and tables that means you are not writing for database you are writing according to the business requirements everything domain-specific whatever you return for the domain-specific and business requirements for that it entity framework will generate all the database and tables so in a code first approach of entity framework we are going to create a database and tables for by using the dome specific classes let us see how we can handle this code first approach for that I will first show you our database management system SQL Server management studio in SQL Server databases in my machine you can observe we have the databases category and a list of your databases here available in my machine now what we will try to do is we will try to create a database by using business specific classes here you can observe the database these are all the various databases available we will try to create a database called Northwind any any database you can do but I am using this Northwind database because it is a traditional database used by Microsoft for categories and products like information to manage and maintain so we will create our own Northwind database and the database will contain a list of categories and products so we are trying to create a categories and products table we are trying to communicate with the categories and products table in octave in database by using our domain-specific classes so there is no database here you can see in the list by name Northwind we will keep this like like that so that we are now going to create the database with our business logic let us do that so I will keep my database aside switch to visual studio and week we are working with an empty MVC application here in this empty MVC application already we discuss it in our MVC sessions how to create an empty MVC application in MV empty MVC application the application is not enabled with entity framework you have to install entity framework for your application to use you can see in the references of this entity framework code first approach in the references there is no entity framework initialized so you have to install entity framework in order to use entity framework in an empty application let us first install entity framework to install entity framework you can download it from Newgate packages so right click on references in the references you will find manage NuGet packages select manage you get packages it will open a new gate package manager and in the new gate package manager you can go to the category browse because already some packages are installed here but in the installed packages we do not have entity framework so we will we can such that I will search for entity framework and there is no entity framework it says that no packages form that means in installed packages there is no entity framework then we browse means we will go online who is the package provider you get so it will connect with the official you get dot o-r-g it will download the package from that source so we will go and search online for this package so I will click on browse it is searching for entity framework and you can see the entity framework library is there so we need to install this entity framework so let us select entity framework and this entity framework you can see right side you have an install button you get package will provide the latest version available its the stable version and you can select the older versions also that if you are Visual Studio is using any older version so here I will install the latest version for our EM easy so click on install button so when I click on install button it will first connect with the source and it is asking to review the changes that means it is asking to confirm whether to install or not I will click on OK this will ask us to accept the terms from Microsoft let us accept the terms now this will download and you can see in the status it is adding the entity framework package to your application and the once it is successfully added then it will show you the message finish it no errors are there in installing the package so we installed entity framework for our visual studio so that we can start working with entity framework now let us check in our project whether this is available or not so first I will switch to the solution Explorer in the solution Explorer go to references and in the references you can see we have the entity framework library available now we have the entity framework library available so now application is ready to use entity framework so the first step in our approach is create an empty MVC application and install a do dotnet entity framework for your application you can install from you get package manager you can go online search for entity framework and download from the new get package manager so once it is installed once confirmed whether it is added or not you can confirm that from the package config file you can go to the package config file in the package config file you will find the list of all the packages that are available here and you can see the entity framework is also available here to use so the first step is ready that means you application is ready to use entity framework so now what we want to do exactly is now we want to use some business classes we want to write some domain classes these classes will be responsible for interacting with the database but database is not yet ready database is not there now by using the domain classes these business classes we are going to create the database and the tables if database and tables are later ready these classes will be responsible for interacting with it the default approach for entity framework is code first that means it will try to create the database let us see how we can do that so first to communicate with any data source we have to write the connection strength in the web config file so I will switch to the web config file you can see this is the web config file in the web config file I will write the connection string here so how do we define the connection strings we can add the connection string I am adding the connection string here connection strings in the connection strings we are adding a connection string add name name of the connection string I will give as a north wind connection north wind connection is the connection string name and the provider provider is our SQL server then system dot data dot SQL client and we have the connection string connection string data source data source is the PC name server name my server name is admin PC ok and we want the database name as initial catalog ok the database name here very important is there is no database we are assuming that database will be by this name so I will give the database name as Northwind dB okay that is the database name authentication user ID sa and the password is one two three that is fine so that means what we are exactly doing here is after installing entity framework we are writing the connection string in the web config file the connection string name is Northwind connection and the provider name is a SQL client connection string is datasource my server name initial catalog is not windy B and C the interesting point here there is no not windy B already in my databases so we are giving everything as per the business requirement so that means later it will create a database by name not windy B which will have all these database and tables whatever you are defining in your business logic and its using the user ID si and password one two three okay once you define the connection strings we need all the classes that are responsible for in the databases and already we learnt in our ABC framework that the classes that are responsible to interact with the database are defined in a special folder called models so I will close the web config file let us go to the folder models you can go to the folder models now in the models folder in the models folder we need to add the classes that are responsible for interacting with the database so in MVC framework model contains all the data access logic communication with database representing the database all these classes will be here now in our database we want two tables one is for categories and other for products so we need to create two classes those will represent the categories table and the products table so let us add a class in the models right-click on models go to add and select add new class let's give a name for the class I will give the class name as a category category is the class name ok let us add this class into models now the class category is defined in the namespace entity code first dot models so what this category class is doing here it is going to represent the categories table in the Northwind database so what fields you want in the Northwind database that you have to define here so I am adding some properties one property so is a public int category ID so let's set it as a property get set accessors another one public string name it get set accessor okay so let's give it as a category name category name so we have two properties in the categories one category ID and category name now our intention is this class should map to a database table in sequel server how a c-sharp class will map to a database table how these fields will map to those database fields table fields for that we discussed about a namespace a separate library that is data annotations data annotations will provide some set of attributes which are responsible to design the classes in such a way so that they are mapped to the database tables now we have to use those attributes and design our class in such a way so that it will map to a database table so I need two classes here to use so I am importing those names pieces for them using system dot component model component model dot data annotations dot schema in our previous session earlier session of entity framework we discussed about component model data annotations to annotations are their schema annotations model annotations schema annotations are responsible for defining the structure of the table model annotations are used for defining the model fields so we need to name species here schema another one using system dot component model dot data annotations now the first important is this categories table should map to a database table then I have to mark with an attribute called table I will define the table name as TBL categories categories so what is the important point here is the categories table the categories table is going to have the fields like category ID and category name the name of the table in the database will be TBL categories so you created a class called category but it is mapping to a database table called TBL categories and in the table we want category ID as the primary key then I will mark this category ID with an attribute key so that means now this is designated as a primary key in the table so what is the database to be created database will be created by name Northwind and in the database one Cable will be added by name TBL categories this table will have two fields category ID and category name where category ID will be designated as the primary key so we are creating our domain classes which will map to a database table if the table is there it will interact with the table if it is not there you can define the strategy in such a way so that it can even create the table okay let us add one more table for that so I am adding one more class into models this time I define the classname as a product one class four categories another class four product I am adding one more class for the product okay now this product class should have some members like public int product ID let us use a gets a tax assure for that another string name so get set and another decimal so let us put a decimal type price and gets it access er okay so that means now products will have three fields one is product ID the product name and the product price now let us map this to the database table then say using system dot component model dot data annotations dot schema another library using system dot component model dot we added the data annotations one component model data annotation schema another one component model data annotations and let us mark these annotations I use those annotations and mark this that so that it will map to the products table so same as we done for categories so I am marking the product table with a table attribute and let us put the table name as TBL products so one table is TBL categories another table TBL products I want product ideas primary key then I will mark this as key now comes very interesting and important point here is every product in the products table belongs to a specific category that means in the categories table we have category ID and category name if there should be a navigation property between the products and the category that means I want to access the category ID as a foreign key in the products table how you will designate a foreign key in a c-sharp class that's an interesting point here so the products table what we want is we want a field called category ID and this category ID should resemble a foreign key that means whatever the primary key of categories table is it will should it should come here as a foreign key for navigation so how you will designate a foreign key here let us see a simple mechanism for that so I am making the access public and I am creating a type of category category and then let us name this as category so and a simple get set access it so I am creating a property category this category property is of type category that means whatever the category ID that you whatever the primary key that you defined in this category stable it will be used as a foreign key in this table in the database tables actually this field will be referred as category underscore category ID that means the foreign key that you want to use in your table that you can refer by using this method so just you have to create a type type of what type it is a category type when you create a category type that means there is referring to the foreign key of the category stable so exactly see what is happening here this category is referring to a category type and whatever the primary key is there in the category the primary key in the categories category ID this will be referred as a foreign key in the products table so how to designate a foreign key here just we have to create a type that a property of the type that resembles the primary key of another table so then it will access that primary key of the table as a foreign key here so the products table is also ready and the categories table is also ready we defined two important classes one is categories another one products so through these domain classes we are going to design two tables one for categories and products now we have to design a context already in our entity framework libraries we discussed about three important classes dbcontext database and DB set DB context is a collection of DB sets you created two DB sets now one is for category another for products now these are tables now you need one context class which is responsible for communicating with the database so in the models already we are having two classes one for category another for product this is resembling the categories table and products table now we need one more class which will be responsible for communicating with the database so I am adding another class here add a new class and let us define the class name as a class name as Northwind context Northwind context now very important here is this context class is going to communicate with the connection string that you defined in the web config file so I am adding a context class so this context class should inherit the DB context type so we are importing a namespace using system dot data dot entity so this entity will provide you all the entity classes DB context DB set and database so why you are creating a context class because context class will provide all the properties and methods that are responsible for communicating with a specific data source you designed tables but you haven't mentioned how to communicate to which data source it has to communicate the data source and all other descriptions we defined that the the source and all the descriptions we defined in the web.config file that means now this Northwind context should use a connection string defined in the web.config file to communicate with the database how this context file will communicate with the connection string for that it has to implement a DB context you can go to the definition of DB context and see I will put the cursor in that and press f12 function key you can see that DB context is a class which is having so many constructors overloaded with various functionalities and here it is having a constructor which is accessing the DB connection and another constructor which is expecting the name of connection string that means what we can do here is we can access the connection string by the reference of DB context so what we have to do here is create a constructor we have to call the constructor of base class here how do we call the constructor of the DB context here so we will create a Northwind context constructor so this constructor must call the constructor of base class where we have to pass the connection string how do we call the base class constructor just we have to use the keyword base and here we have to pass the name of the connection string the connection string name is north wind connection that is the connection string name that means now what we are doing is we are accessing the DB context we are passing the name of the connection string through which you want to connect that means whenever you create an object for this Northwind context this object will be responsible for communicating with the connection string defined in this name that means in the web.config if there is a connection string called Northwind connection the Northwind context object will be responsible for communicating with the connection string you can see that now here Northwind context is using a name northwind connection and in the web.config file let us see the web config file in the web config file there is a connection string by the name Northwind connection you are referring this name in the context class so what a context class object will do context class object will be responsible with the data source that you have defined in this connection string okay after connecting with the data source it has to interact with two tables one category stable and other one products that means now it should have two DB set one DB set is of what type category type I will leave it as categories table okay it will be responsible to read and write the values from the categories table another DB set is a product table and let us put the name as products table table and it will be responsible to get and set the values into it now the DB context is having two DB sets one is category stable another one products stable so you can communicate with the categories and products table by using these two DB sets but these two DB sets are defined in the context class that means in order to access them you have to create an object for this context a context class object can access this DB sets one is category stable another one products and all our model classes are present in the library entity code first dot models so all the model specifications are ready now let us run this and see how this is going to work now so exactly what we done here in the models we created three important classes one representing the categories table another representing the products table another is responsible for communicating with the database that is the context one so now we will add a controller let us add a new controller you an empty controller it is an empty controller let us name the controller as some northwind controller okay in this Northwind controller by default there will be some actions there will be an index action we will use that index action to return some values so in this Northwind controller by default we are having an action index which is returning a view so we have to access the model process here so I am importing the library using entity code first dot models this will give access to the models whatever all available in the models you can access them in by using this library so what I want to do here is in the Northwind controller first we need to establish connection with the database and establishing connection with the database is the responsibility of context class so I have to create an object for context class our context class is Northwind context DB is equal to Northwind context so we are creating a DB reference so which is representing the Northwind context that means now DB is responsible for communicating with the database that is defined inside this connection stream now through DB I want to access to tables when index is invoked DB dot one categories table and return the list of to all the values present inside it another we want products table dot to list return the list of all options inside the products table and remember the important point is still there is no database there is there are no tables now what you are trying to return you are trying to interact with the database and tables which are never exist so what you are trying to do now is you are trying to create them by invoking them in as a list so let us add a view for this index action I am adding an absolute empty view for the index action and in this view we will add a simple message that database is created or something else why because whenever I invoke this Northwind controller index action hot wind controller index action will create a reference for DB through that it will try to communicate with the tables that is category stable and products so here I am just adding a simple heading saying that the database is created okay now let us run the index action and see what happens when you run the index action actually this index action will try to communicate with the database that you defined in this context and it will try to interact with the tables categories and products and interesting point here you can see in our SQL databases still there is no database called Northwind I will refresh there is no database called Northwind you are writing all the business logic after writing the business logic let us run this and see now so I will just run this index action and see now when it tries to invoke the index action first it will try to establish connection with the database but it will find that there is no database in that specified location and entity framework by default it uses a code first approach in that situation if database is not found according to the specified options then it will create the database and it will create all the tables that are defined in your domain classes let us see how it will create the database in tables so when I run the index action it is trying to invoke the database and it is trying to interact with the tables but there is no database and tables there to interact then entity framework will use its default approach and what is the default approach code first in code first approach what it will do is it will create all the database and tables that are defined in the domain specific classes of your application let us see how it will be done I am running the Northwind controller here I am running the Northwind controller index action this Northwind controller index action will invoke that index view and it will get me the output okay now you can see it's showing the message database created now let us see what exactly happened in the database so I will switch to the SQL Server management studio and see in the databases I still have the same databases that I showed you before we started this example now let us once refresh the databases and see and you can interestingly find the database created by named Northwind and this Northwind database you can expand and see there are tables and we have two tables one is categories another one products and let us see what the fields generated into the categories table into the categories table there are two fields defined here one is category ID and category name and category ID is designated as a primary key and see the products table in the products table interestingly you will find the category ID as the foreign key so that means the product ID is the primary key name and price and the category ID is the foreign key so what exactly we done here is by using our domain-specific classes we created a database so database in tables so what is the entity framework code first approach the entity framework code first approach where you can write the domain specific classes and entity framework will generate the database and tables for those classes if database and classes are ready then it will try to communicate with the database in tables so this is one of the approach of entity framework in our later sessions we will see how to use a database first approach and model first approach keep watching nourish ID videos and please subscribe for more thank you
Info
Channel: Naresh i Technologies
Views: 49,440
Rating: undefined out of 5
Keywords: ADO.NET, Sudhakar Sharma, Naresh IT, Hands on ADO.NET Training, ADO.NET Demo, Online ADO.NET Training, ADO.NET Tutorial Videos, ADO.NET Overview, ADO.NET Interview Questions
Id: UuZKXAVneag
Channel Id: undefined
Length: 35min 54sec (2154 seconds)
Published: Tue Sep 20 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.