Entity Framework Model First Approach | ADO.NET Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to nourish technologies this is the question in our previous video sessions of entity framework we discussed about two different types of approaches one is the code first approach and data base first approach in code first approach according to the domain-specific classes we were able to create the database and tables in database first approach if already database is there and the tables have generated now we are generating the classes from the database and tables to interact with the database now we will see the third types of modeling approach that is model first in model first approach we prepare the visual designer according to the visual designer entity framework will generate the domain-specific classes as well as the database and tables so in this entity framework model first approach we are first going to prepare a visual designer according to our entity model the general database models we have four different types of models conceptual physical logical and entity according to the entity model we will design the visual designer then entity framework will use that visual designer to generate the classes as well as the database so in this model first architecture we will have a DB model this DB model will be used by the entity framework to generate the classes as well as database tables let us see how this approach works so first I will switch to visual studio in visual studio I am creating a new project it's an empty MVC project it is an empty MVC project with models controllers and views so first we are going to design a model so right click on models and add India dotnet entity data model we are selecting an ad or dotnet entity data model that means we are creating an EDM X file in this EDM X file we prepare the designer and entity model so that the database tables and classes will be generated according to the designer you design so model actually comprises of all the classes responsible for interacting with database but if this moment we do not have any classes and no database available so we will first create a visual designer so that for the visual designer it will create the database and tables so let us add an ad or dotnet entity data model and specify a name for the data model I will give the data bottle name as some trains DB model so trains DB model so this is an entity data model generally we already know that entity data model comprises of three important components the conceptual model and the storage model and mapping conceptual model will represent the data we are working with storage model contains the data and mapping contains information that specified how the conceptual model will communicate with the storage model so we are creating an entity data model that will comprise of all the three components means it contains the conceptual model the storage model and mapping so let us add the entity data model by name trains DB model okay now it is prompting you to select a DB model type that you want to use so we have already worked with designer from database that means what it will do is if already database and tables are there it will prepare the designer but we do not have any database and tables now now we are using here an empty code model first here we have 3 options code first model where we have code first from database but we want to just design the designer so that for the designer it will create the database and tables so I am selecting an entity framework designer model so what this designer model will do is it will provide an UI where you can design the entity model so it creates an empty model in the entity designer so that you can add the entities design the entities configure the relationships according to configurations it will generate the Poco classes as well as the tables so we first selected an entity data model entity data model we given the name estranged DB model and it is prompting you to select what type of model you want to use we are selecting an empty EF designer model after selecting MT EF designer model then click on finish this will add a visual designer into your project let us see the visual designer how it looks like and how to design the entities in this visual designer so I got an empty visual designer here let us go to the solution Explorer and see here you can observe in the solution Explorer initially now I am having a trying DB model EDM x-file in this strange model admx file we do not have any classes generated generally we already discussed that entity data model will use two different classes to communicate with the database and tables we need a context class and a model class as of now there is no context and model but it is just having a diagram our intention is to create those classes according to the visual diagram and here this is the visual designer area in this area you can prepare the entity model for your entity model it will generate the database as well as the code to design the entity model you can go to the toolbox and you can see here toolbox provides the options like entity inheritance Association you can even maintain the relationship between the entities or you can use this toolbox or just you can right-click on the designer so right-click on this empty designer in this empty designer you have to just select add a new entity so we are adding a new entity and let us define the properties for the entity the first important property is asking for entity name let us put the entity name entity name by this name the table will be generated into your database so let us see the entity name I will define as TBL Trains TBL train is the entity name that means a table will be created into your database by the name TB of train and what is the entity set named entity set name will be pluralized that means it will be trains because this entity set is responsible for generating a DB set so that you can read and write values into the table so entity set will generate a DB set which is responsible for reading and writing values into the table so the table name is TBL train and the entity set name is tbo trains and it is prompting for the key property means what is the primary key in your table you have to define the primary key property I will define the primary key property as train number train number is the primary key property and what is the data type for that property so we will define a data type for the property you will have all the available data types I will in the list you can select any data type initially let us select in 32 so we are adding a new entity to the visual designer the entity is representing a database table so entity comprises of several properties in these properties first we define a entity name which will be the table name entity set name which will be a reference name to interact with the table and the key property train number which will be designated as a primary key field inside the table so let us add this entity now you can see in the visual designer in the visual designer an entity is added by name TBL trail and the entity is having a property train number now we can add the properties into this the properties as well as the navigation properties if required so let us add some more properties into this entity how do you add the properties into entity right click on the entity go to the option add and select adds color property so we are adding a new scalar property if you are defining any navigation property through relation you can define a navigation property so initially we are adding a scalar property and the scalar property name I will define as a train name so we have two properties one is train number another one train name okay next I need one more scalar property into the add another scalar property this time I will define the scalar property as departure departure is the time I need to define then I need to define a data type by default whatever the entity fields you add into entity there default data type will be a string type in order to change the data type of that particular field then you have to right click on the field name and select properties in the properties of the particular field you will get this properties window in the properties window you can define the properties for that particular field and the properties are general entity model properties like you have max length you have min length you have fixed length you have a the name is it nullable or not and finally we have a data type so we here we are selecting the data type instead of string we are selecting the data type as date/time so you want the departure to be dead time so we need to define the departure time okay we need to add one more property add another scalar property let's put as destination plus nation and let it be the general string type property so an entity is defined inside the visual designer you can add any number of entities you can define and configure the relationship between the entities in this visual designer after preparing the visual designer once the designer is ready once build your solution after building the solution let us go to the solution Explorer and watch the entity DMX it is still having only the diagram so no domain-specific classes are created so business classes are not yet generated and we haven't even created a database now one of the great feature about entity framework is you prepare the visual designer then for this designer it will generate the database as well as classes now how do we create a database for this visual designer let's like click on the table in the visual designer right click on the entity name and select the option generate database from model so now we are trying to create a database from the model so model is our visual designer entity so we have selecting generate database from model okay let us configure the model means we need to define the data source name we need to define the database name what you want to create for this model that means with the help of visual designer we are now creating a database a database is created automatically along with the tables for the fields how now select the option new connection and let us define the connection server name so let us use the server name Leno is my server name and authentication SQL Server authentication user ID and password and the database name database name there is no database we want to create a database for our visual designer then I will give a new database name as Trains DB so Trains DB is the database name that will be created if it does not exist if it is already there then it will add this table into the database if it is not there then it will create the database with the specified name now let us click on OK and see it will try to communicate with the server that you define if the server and the authentications are properly defined if everything is went fine then it says ms it prompts a message saying that so you are trying to attempt to create a database by trains DB are you sure you want to continue let us click yes to do that so that means a database will be created by the name trains DB so that it will be used to store our entities so the trains connection is defined and it is prompting we want to include this connection string into your configuration file yes we want to include this connection string into configuration file because we want to use this connection string later in an any another requirement right next step it is prompting to select the entity framework that you want to use version number obviously we will select the latest version available on our machine then next step in the next step it is asking to define the SQL query file name that means now whatever the database and the tables that you defined in the model for that it will generate a query file so what is the advantage of this query file whenever any change occurred our development that directly we can run that query file inside the production server readily available to the clients so this query file will be very important so a query file is generated for all the specifications that we define along with the database name table name and the fields we need to just add that query file into the project now whatever the configurations that you defined through a visual designer for that it will generate a query file so we need to run this query file so that the database and the tables are generated according to the query define this query file is an SQL query file which comprised of all the commands that are responsible to create the database and tables how these commands are generated according to the model that you have defined in the visual designer right so in the visual designer we prepared a entity and from the entity now we are trying to generate a database and tables now you can observe here in the project it will generate the query file here just execute the query file so I will go to the execute button and execute the query file okay then it will ask you the authentications if required to execute that query file on your server so I am defining the server name and I am defining the authentication type and user ID and password yeah and the database name the database name is Trains DB so we will select the database as Trains DB then connect so it will try to connect and it will execute the query and it shows the message query completed successfully so what might happen here so it will use these queries to create the database end tables let us verify once build your solution after building the solution let us go to the solution Explorer and see what is happen in the admx file and interestingly in the EDM X file you can observe now the domain classes are generated for the visual designer you prepared see the domain classes now we have a class called TBL train remember this TBL train is a model class that is automatically generated according to the visual designer you define and let us see the TBL train class this TBL train class is having the members like whatever the members we define well like train number train name departure and destination now observe the context class context class also might be created here let's go to the context class and see this is the context class it will be in a DMX context template and the context class is using a DB context and it is using this connection string to communicate with the database and see the DB set name it will be TBL trains so you just prepare a visual designer for that visual designer all these domain-specific classes are generated now what about database let us look at the database so we will open our SQL Server and verify whether the database by name trains is generated or not remember you are creating a database and adding tables into the database even you can see the data into the database tables by using entity framework now this is a model first approach that means you haven't prepared the database you haven't designed the domain classes you just prepare the visual designer let's log on to our SQL Server and see when I log on to my SQL Server and the check in the databases list you can observe there is a database called trains DB and in this database we have a table called TBL trains and TBL trains is having all these columns like train number it is designated as primary key and train name departure and destination and all the data types are defined exactly where we defined inside the model so what happened in this model first approach in this model first approach we prepared the visual designer by using an empty data model by using our entity framework admx and for that visual designer entity framework generated the classes and it generated the database end tables so this type of approach is called as model first approach so what is the three different types of approaches available in entity framework these are all the three different types of approaches we discussed all these approaches one database first approach where we will have the database and tables ready for that entity framework will generate the domain classes - it is a code first approach where you define the business specific Poco classes for that entity framework will generate the database and tables and the third that we saw today that is model first approach in the model first approach we just prepare the visual designer for that it will generate the database tables and the domain classes these are all the various types of approaches of entity framework so with this we have seen one simple concept of understanding what are the various techniques involved in designing of database and communicating with the database by using entity framework in dotnet in our later sessions we will use this entity framework and learn how to handle the crud operations that is to create read update and delete the records into the database thank you and keep watching nourish ID videos
Info
Channel: Naresh i Technologies
Views: 12,018
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: qPYr-tNR1EU
Channel Id: undefined
Length: 22min 4sec (1324 seconds)
Published: Wed Nov 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.