Entity Framework core 7.0 ( DB First Approach & Code First Approach) | .NET 7 Updates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to Nigeria techies this video we can talk about Entity framework core in dotnet 7. so we can start from what is Entity framework core so Entity framework core is an object relational mapping it is provided by Microsoft so the latest version is 7.0 the latest version can support the dotnet 6.0 and 7.0 and also we can work with different types of databases it includes SQL databases MySQL and azure so the Entity framework core provides three approaches to create our entity model the first one is database first and the second one is code first and the file loan is model first so all the approaches having some problems on consonants So based on our requirement we can select our approaches so most probably people are choosing this database versus approach and also this code first approaches so if you have the created database already we can go with an database first Approach at the same way if you have the created data model classes in our application then we can go with our code first approaches for this video I am going to explain this database first approach and also this code first approach with some proper coding examples now let me go to my created application so this is one of the.net core PBB application so the first thing is we have to install the required packages for our Entity framework core so here I mentioned these four packages that we supposed to install in our application the first one is Entity framework core then design SQL server and the file lonest tools so we can install everything on by one just right click manage nuket packages so in this browse section you have to type like a Entity Framework so we can install everything one by one choose the latest version 7.0 so next I am going to install this design and then this SQL Server and the file on these tools okay our installation gets completed so as I mentioned we can start from our database first approach so the database first approach enable us to create an entity model from the existing database so this approach help us to reduce the amount of code that we need to write so using the scaffold command we can generate our entity models so here I just provided the structure of the scaffold command okay if you noted here first we have this scaffold DB context after that we have a lot of parameters that we can discuss one by one first let me take one sample scaffold command okay so if you noted here so we have this scaffolded database context after that we have our connection string then our provider the provider point of view I'm using this skill server so finally I am going to generate my models in the full draft models okay that's what I provided like output directories models okay so now we can execute this command so before that I am going to do one more customization so if you are providing the connection string these are the sensitive information so if you directly providing from your command so this is not the prepared option so we can move this connection string into our app settings.json file so in this application app settings we can include this connection string then we have to provide our connection string name so then we have to provide our value okay this is done let me save this one and now let me go to the command so instead of this direct connection string we can use our Define the connection string from our app settings.json for doing this one I am going to use the properties called connection okay add them we have to provide this name so let me take this name connection string okay so let me copy this command so in this application let me go to our package manager console it's already opened I'm going to execute this one so the Bullet is charted see now our database model is created see the folder here and also we can see our database context here okay so let me go to my SQL Server so this Lan DB only I used so here I have around eight to nine tables so all the tables are included in our model section so next what I'm going to explain so instead of generating all the objects I have to generate the specific table only for the example I am need to generate the model for only this customer and employee okay first I am going to remove this model here now let me go to my command so after our output directory I am going to provide one more parameters table okay so here I'm going to include two tables the first one is table customer so comma table employee so if you want to include some more tables just use the comma separator then you have to include the table name okay now let me copy this command I am going to execute here once again see now again our model is generated So currently we have only two objects only Even in our database context also we can see the same information okay so this is the one scenario and the next thing is So currently I am not provided the database context name okay so if required we can provide the database context name also and then we have generated all the files in the folder of models so instead of that we can generate our database context in one folder and other class files we can generate in this model folder that also we can do so let me delete the folder once again and we can change our Command here so in this case I'm going to remove this table option okay I'm going to provide context so context name is DB first context okay and then I am going to include the folder so context directory for generating our models I am going to use a repost last models okay so our Command is ready let me execute this one okay see now again our folder is generated the main fault is wrappers so if you see here so inside the reports we can see this database context file so in this database context file we can see all the objects then in the folder of models so all the individual models was there okay and the next thing is so currently I just opened one of the model file so we have the properties right so here we can include the data annotations also so if you want to include this data annotation you at the end you have to provide this data annotations okay so let me copy the same command now I am not going to remove this model so here after we can have this models because the structure is fine so the Bullet started see we are getting the error so already we have this file okay so for the all the models we already have the files so again I am trying to generate the models within data annotations it is throwing this error for resolving this error we have to include the flag of force okay so in this command I'm going to include force or else we can provide the short name is F okay so let me execute this command you know it is working fine let me check my models see I just taken so the table category and the key is included the similar way the stable employee so key column and the length so all the data annotation related properties are gets included okay now the very important thing is we have to update our database model right so let me take this table user I'm going to include one more column so now we have new columns in our table so I'm going to refresh our databases okay for the referencing purpose we can execute the same command so if you are including this Force so automatically it will recreate all of our models okay so let me copy this one I am going to execute the same command okay it's almost going to complete yeah it's fine now let me go to our table user model so see the column of countries included the same way I'm going to remove this column okay again we can do the same thing only so let me copy this one okay the build is completed again I'm going to open this one see so we have up to this effective the country column is cut removed okay we almost done so in this database first approach scaffold commands are doing the wonderful role here if you clearly understand these commands then it will be very easy for you okay so let me provide some short summary so as I mentioned the first one is connection used for forward the connection string so even in our comment also we can see this connection string and the second one is provider so in our example we have used this Microsoft Entity framework core dot a skill server as our provider and the output directory generate the model in specific folder so we have generated our models in the folder of models so inside the refers folder adult add-on is context provide the name of database context so in our last example we have provided our database context name at the same way for generating our database context in the particular folder we have to use this context directory and this table so generate the model for specific tables so instead of generating model for all the tables so if you want to generate for some required tables then we have to use this option for the data ancestation we have to use this one the file only firstly so you have that created database context you are trying to modify the existing file then you have to go with Force so I recommend for Force for every time so whenever you have the database changes so you have to execute the command with the force it's automatically recreate your model so now we can check the connections whether it is working or not so for doing this one I'm going to create One controller here we can create one empty controller so I'm going to provide the name as customer okay okay our controller is ready now let me go to our database context okay if you noted here we are included this connection string it's not required because we are going to include this in our dependency injection let me go to our program.cs file let me register my database context here so our database conduct name is DB first context okay next we can use this Lambda expressions so option dot use SQL Server so here we have to provide our connection string name so we can get from our configuration side so Builder dot configuration get connection string off so the connection string name we can get from our app settings.json okay next I am going to inject this database context in our controller so first I am going to create one Constructor gets fine so let me enter here okay this is done next let me create one function here so now I am going to run this application we can verify whether we are able to fetch the data okay our guitar method is working fine it is returning some data that means our connectivity everything is good only so using the same manner we can create the separate action methods for our thread operations so that I will cover in the separate video so this video my primary intention is explaining this Entity framework core with the different types of approaches so the first one is database first approach so we are almost good now let me move on code first approach so if you have the created data model then you have to go with this code first approach so that's what I have mentioned here the code first approach enable us to create the model and they are lessons using classes then it is creating the database from these classes okay and then I have listed out all the steps for this code first approach when we have to go one by one so the first thing is I am going to create on separate application so the steps are same only it's not going to take much time with BBA the project name is code first okay our application is ready so in this code bus stop brush the first thing is we have to create our data model so in this application I'm going to create one folder first model then I am going to create one CS file for the data model I'm going to provide the name is product okay here I'm going to declare the properties here the first field is ID the similar way I am going to declare two more fields the second one is name then price so this is a very very basic one okay so now our model is ready next we have to install the package so in this case we have to install only this Microsoft Entity framework core dot design online okay the installation gets completed so the next thing is I am going to create One controller here baby APA APA controller with actions using Entity Framework here we can choose this product model and the database context we don't have anything so let me create one okay the suggestion is saying code first contacts yeah we can use the thing and our controller name is products controller let me add this one see now it is installing this Microsoft Entity framework or dot tools and also it will install the SQL Server also okay it's added okay our control is created with the credit operations see this HTTP delete Force to put everything was there and also in the app setting.json so the connection string is included add in our program.cs file the dependency injection related codes also added okay now our next thing is we have to execute this add migration command Okay so it will generate the migration file for us so let me copy this command so in our package manager console let me go to the tools then you get package manager package manager console okay I am going to provide the name for this file okay it is generated the migration file see the name and here we can see the items for the product because the columns ID name tries so the next thing is using this migration file we can generate our database for doing this one we have one command is called update database let me execute this one okay this is completed see so it's executed this command so it's created on table So currently we don't have any data that's what it's not included anything so if you want to see the database okay so in this connection stream we can see like a local DB okay so just to go to this view in our Visual Studio we have this SQL Server object Explorer so in this csql server side we can see the same thing MRC SQL local DB so in this database section code first.data so that's what we have provided so in case if you want creating the different names that also it's possible from here so let me change this one I'm going to provide test underscore DB okay but I'm using the same server I just changed this database name only so let me save this one so the steps are same only first we have to generate our migration file okay the file is generated now let me update our database okay actually it should be update database okay see the migration is completed I'm going to reverse this one see now this test DB is included so inside that in this table section we can see our product table okay so if you want to check the data you have to give view data currently there is no data so what I'm going to do so anyway we have this credit operations so let me run this application we can insert some data here id1 okay the identity instead of so this is the identity column okay well I guess you know the first record is created so we have two records so let me execute this guitar function see no two records so even though we can check from our databases also let me go to the skill server once again I'm going to click this view data see now currently we have these two records so actually I have to include manually also so bar 50. let me save this one I'm going to execute our API so you know it is returning three more data so this is all about using this local databases now what I'm going to do instead of this local database server I'm going to use MySQL server okay the step is very simple you just go to your connection string so instead of the server you have to provide your server name okay I just do changing my server name first and then we have this database it's fine because I have the database test DB okay So currently there is no tables are added and this tested connection and other things are fine only so let me save this one I'm going to generate the migration file once again so the name I'm going to provide in it too okay okay this is fine next I am going to update our database okay we are getting the login process we are getting some issue for resolving this error so in your connection string you have to include one more property okay trust server certificate equal to true so let me save this one I'm going to update the database once again you can now it's completed we can check from our SQL Server I'm going to refresh this one see so there are two tables are included first one for the Entity framework migration history so if you checked here select top 1000 row so the migration was happened then in our table so table also created with the same structure we have these three columns okay so it's working fine so here after if you are including one more new columns the steps is very simple I'm just added exactly okay I'm going to save this one so for any changes in our data model you have to generate the new migration file with some spelling mistake right okay the file is generated so in this file we can see the new column is included effective and then I am going to execute our updated database okay it's completed now we can check in our SQL Server so three columns I am going to refresh this one so see our exact column also included so this is for adding the column the same way so if for the removing column also it will work in the same manner only so now we are almost in the end of the video so I clearly explained what is Entity framework core and this database first approach and the code first approach So based on your requirement you how to choose the approaches so finally let me provide some detail about this NDT framework core 7.0 so basically this Entity framework course 1.0 can support for dotnet 6 and a darknet 7. so anyway we are using the latest version both side so then it's fine and this 7.0 having very good performance comparing to 6.0 it is four times faster while doing the save changes and the save changes are synchronous and we have the options like execute update and execute delete so for doing the bulk updates and also we can insert the multiple rows so these are the new features are included in this Entity framework course on point zero so still if you have any doubts or clarification please post in the comment box and also please don't forget to subscribe my channel thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 23,748
Rating: undefined out of 5
Keywords: EF Core 7.0, Entity Framework core in .NET 7, Entity Framework core with db first approach, Entity Framework Core 7.0, Entity Framework core with code first approach, Generate entity models using scaffold command, How to generate model for selected tables, how to update the existing dbcontext, how to generate entity models with data annotations, Generate dbcontext and models in specific foldar, what add-migration in EF Core, update-database in EF Core, new features in EF Core 7.0
Id: TE1Hpi1SSaE
Channel Id: undefined
Length: 29min 10sec (1750 seconds)
Published: Thu Dec 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.