Build CRUD with .NET 6 Web API & Entity Framework Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends thank you for watching this video i am muhammad and based on a lot of the community feedback that i have been getting as well as a lot of the requests that have been receiving we're going to be building a 0.06 web api with the fcor we're going to be building a cloud operation in order for us to cover all of the aspects of our web api as well as af core integrations so grab your cup of coffee and let's get started so what tools are we going to be using in order for us to accomplish this well if you're on a mac you have visual studio preview which you can utilize you have visual studio code and you have rider i think these are the best three options for you for this today's video we're gonna utilizing rider so let's get into it first let's open rider and basically what we're going to be doing is we're going to be clicking on new solution and within the menu here we're going to be utilizing asp.net core web applications because that's what we want to do and we want to basically give a name for our project and that's build a formula one up because it's the best sport so we can say formula one [Music] project name formula one where are we gonna save it we're just gonna click here we're gonna put it on the desktop inside the work in progress we have a folder called project and we're gonna put it there do we wanna put solution inside no we don't want do you wanna create a get repository uh that's uh if you if you want to push it to github we can uh but for now tell me what why not because we're gonna push it either way to get and now it's asking us uh if you wanna choose what type of sdk so if we click here we can see that i have different version of the network sdk i have version 3.1 5.0 6.0 and 7.0 so 7 is still in preview so we don't want to use that and the latest version of the dotnet sdk is 6.0 and that's the one i recommend you using and it's an lts version so it will last you for a few couple of years so let's use the nut six and here it asked me what type of uh project i want to create so empty it's gonna basically create an empty web application grcp service which i have another video scoping grpc which basically it's going to allow us to do some similar to an api but it's allowing us to do communication for example between machine to machine there is a class library what is it web api that's what we want to do we can for example here a web app we have a web app with a mvc and we have one with angular and react so i think for us for now we're going to be utilizing a web api one and then here it's going to ask us what language do you want to build it with of course we're going to be utilizing c sharp we see here that we have the framework already chosen for us which is going to be that not 60 framework do we want to add any authentication at this moment in time we don't want to add any authentication do you want to add the occur support out of the box we don't want to add any docker support and if we go a bit down we can see here this is going to be the basically project structure for now so that's all good if we see here we can see how the project information we can see it's gonna be a web api we're gonna be utilizing version six and basically we see also it's to be the template that vitalizing is provided by microsoft which is the best scenario for us so we're just going to click on create and right now we can see that our solution is loading let's make this a bit bigger perfect so as we all know when dot net 6 came out there has been a lot of changes to our project structure and one of the main thing is basically we have removed the dependency on the startup class so we can see here that we have the program.cs which is going to be the main section of our application we don't have the startup to see us anymore and because we build this application without relying on minimal apis so we have basically normal controllers that we can utilize if we have utilized minimum apis we're gonna uh we're not gonna have the controller folders available for us from the cut go so that's something you need to pay very attention to but for us we're gonna be utilizing the controller approach for our web api and let's take a look at our application before we do anything else so first of all if we open the program.cs what we can see here is we're basically utilizing the builder we're adding a controller service because as we said this is a web api with controllers we're having basically utilizing swagger in order for us to have the open api interface so we can utilize it and we basically we're building the application and if our application is under development which is basically locally here at this moment in time basically we're gonna we're gonna be able to utilize swagger and swagger ui and then here we're enabling https authorization controller and then we're running the application pretty straightforward stuff so let's run this application and see what's gonna happen so if i click on debug let's see the files that has provided me do we need any of this right now uh we don't need this we don't need this we don't need this escape together nor you can see the project is building because i run it in mode it takes a bit of time so let me open it in my preferred web browser for development which is going to be edge and let me take this url and paste it here perfect so what i can see here is basically i have a single controller and i have a single endpoint which is for the weather forecast and this is basically the default endpoint which is provided out of the box for free when you create an application from microsoft just to make sure that our api has been created successfully and for example if i try it out i click on execute i'm receiving random data which is exactly the expected response which is great so that's all good so right now we know that our application is running and basically we have we are able to execute certain uh actions so let's stop this so the first thing that we're going to be doing is i'm going to be creating a new folder and i'm going to create a new directory here and i'm gonna call it models so just a disclaimer out there what i'm gonna be doing here is i'm gonna be adding all of my resources into a single project so i'm gonna be adding my models into a single project i'm going to be adding my of course stuff into a single project i'm not going to be separating them just for the sake of demoing this is not the best approach if you're building a big application but if you're building a small demo application like this that should be fine but just bear this in mind this is not really the best approach if you're going to building an application which planning to support on the long run or for example it has a work application for example which we are expecting a lot of user to be contributing to or utilizing it so just i wanted to put this disclaimer out there so let me create this folder called models and inside the models folder what i'm going to be doing is i'm going to be creating a class and basically what i'm going to be doing is since it's a formula one app so the first thing within a formula one app is we're gonna have teams so let's create a teams controller a teams uh model and basically inside this team we see here that's automatically taken the the new scope of 1.006 and we don't have the name space brackets directly we have the theme so what are we going to have within the team so properly we're going to have an integer and we're going to it's going to be the id for example we're going to have the team name so we're going to put string and we're going to put the name of the team and then for example let's see we're going to say the country which or basically yeah the country of the team so and maybe lastly say id name country for example and if we're gonna give the team an extra information for example what we can do is should we give them a description yes let's give them a description basically uh this types of input or tell you what let's put the team principle which is team principal name which is basically the manager of the team so i think that should be enough so we have an id we have a name we have a country and we have the theme principle which should be fine so let's save this and once we have created this model the next step for us is we're going to be creating a static controller which means that we're going to be utilizing an in-memory database if you don't know what an in-memory database it means that we're not going to be actually utilizing a actual database it means that we're going to have a set of information available for the application to utilize while it's running but whatever it changes we do to that application as soon as the application stops all of the changes that are gonna go they're not gonna be safe they're not gonna be sustained after the application touchdown so whenever the application starts at that start again we're gonna have to redo the work if any work has been done on it so let's start first of all by opening the controller folder and adding a new class and basically this class here what we're going to be doing is we're going to be calling it teams controller great add and now we have a teams controller created for us i just want to check one thing so if i have here i can see that i have different types of files that i can create so here for example i went with the route of creating a class but on the other hand what we can do is i can just choose to create directly a controller so let me choose that and tell you what let's try this first so let's remove this and uh sorry edit and delete and yes i want to delete and then inside here i'm going to click on add and click on controller i'm going to call it themes controller and once i click and choose teams controller the difference here is basically the generation of it uh it automatically inherited from the basic class that i need to utilize which is here the controllers as well it created a sample action for me which i can utilize which is good but for the sake of this i'm not going to be utilizing the templates i'm going to delete this again and i'm going to create my own so let's go to edit and delete and do it and here i'm just going to go add class i'm going to call it teams controller and that should be it and add it to github perfect so the first thing that i want to do right now is i want to convert this normally class to a controller so how i do it how do i do that in order for me to make a class a controller is i need to inherit from a controller class and that controller class will provide me with all of the utilizations and all of the functionalities that i need in order for me to make my class a controller and there's a lot of there's basically two main ones that i can utilize so i can utilize controller and basically if we can see here it's basically inheriting from microsoft asv.netcode.mvc which basically is going to provide me a lot of different uh controller information that i can utilize so for example i'm having a normal mvc application and i have views i have all of those functionality for for there if i have a web api and i have certain uh actions that i need to do i can also utilize it from there but we can see here that this controller is going to be a lot for me right now because i don't really want to utilize all of the mvc functionalities out of the box all i want is to be able to basically have it as a controller and not recognizing it as a controller and as well on top of that what i want to do is i want to basically utilize the functionalities of a controller so instead of utilizing this this is perfectly fine but for me right now what i'm going to be utilizing is i'm just going to utilize the controller base which is also inheriting from uh the same microsoft.asp.net code.nvc but if we look at the description here what does it say it says a basic class for an mvc controller without a few supports that's what we want because we don't have a view we're not building an mvc application but we have to utilizing the view if we look at back the other one the controller and we see the description it has a base class for an mvc controller with a few support and that's thing that we don't really want so for this case we're just going to put the large controller base because we're not supporting a view and this is a bit these small things will basically optimize our application the more stuff we add to it so that for this reason we're just utilizing controller base so once we have added that we can see it has automatically added the user statement and now basically this class right here is a controller and as simple as that we have created a controller if you believe it or not so once we have a controller ready right now what we're going to be doing is we're going to add some attributes to it and then once we're adding some attributes we're going to be adding our first action so we can see how it works the first thing we're going to be doing right now is above the public class themes controller here we're going to be adding the first attribute which is going to be api controller and you might ask yourself why are we adding this attribute here so let's see what what this information gives us it basically tells that in the case of this type and derived from the http api response and basically when we decorate a controller with it we're basically enhancing the developers experience as well we're telling that uh the controller that we're actually going to be returning http responses with our objects to the person who requested our endpoints so in essence uh first of all uh it basically allow us to treat uh to treat all of our web apis are basically all of our controllers as web apis so because as the first thing that we did here is we basically have controller base and controller base basically removed the support for the views so right now we have controllers which are doing actions but they are not going to be basically returning anything to the views because there's no views what we're going to be doing is because we're not returning reviews we want to return json files we want to return http responses so for example we can return a 200 with a body or we can for example return a 400 error 400 not found with an error message or we return a 500 error message http status called with an error message so on so forth so how are we going to be doing that by adding this api controller attribute on top of this class we're basically allowing it to utilize the http responses and basically make it let's if we go back and utilize their own words when we decorate on assembly all controls in the assembly will be treated as a controller with an api behavior so we're basically we're enabling all of these apis behavior for our controller directly out of the box so that's why we are actually adding our api controller to basically have all of the api's behavior that we want to utilize directly available for us out of the box to this controller that in need so that's the first thing that we're going to be doing so once we have added this the next step is we need to add the route for this controller so what do you mean by route for this controller so right now for example if we are gonna utilize this it's gonna be for example our website dot com forward slash teams and whatever the actions do it and that's fine but that's not the best way to do it for example if you want to build it in a proper way what we want to do is we want to specify within that path that this is an api request so for example we're going to be for for example our domain dot com forward slash api forward slash teams and then we can do it so that's what that way when having this routes it's better for us or for whether anyone who's consuming our controller to understand oh this is an api controller and basically this is not a view and that way and for example i can build my own api libraries and that make it easier for any developer who's trying to consume my api or for me if i'm trying to build my for example my front end i want to see all of my apis within having a single structure of my the routing process it will make it very easy for me so that's why i need to do it so i can put routes and here for example i need to for this one i can put api i'm gonna put forward slash and there's two options so i can put the themes like this which is basically i'm hard coding it or i can put it like this so what's the difference when i put it like this it means that it's gonna take any name that i put here before the controllers uh the word controller so if i put here teams v1 i can put for example the bus themes controller it doesn't really matter it's gonna take it so for example in this case my endpoint it will be api forward slash themes if for example i change this one to teams v1 this will automatically update the team's v1 for example i put v1 for example mercedes it's going to automatically update for v1 mercedes if for example here i put it if i hard coded it here for example two themes no matter what i change here it's not gonna reflect here because this is hardcoded but if i put it like this it's gonna pick up the name directly from here and it's gonna take it so right now what i'm gonna be doing is i'm gonna leaving it as teams and basically this is going to be teams the reason i'm i'm doing it like this is because once we have multiple controllers what we can do is we can abstract this to a certain extent and basically create a base a base controller we can call it base controller for all we want and basically we can put all of this configuration there and we can have other controllers inheriting it from it so we don't have to repeat the work over and over again on multiple controllers so that's basically a way for us to optimize our way and basically have a single place where we change a single file or a single attribute and it's going to be reflected on everything instead of having it manually in multiple places where maybe sometimes we forgot to update it somewhere and forgot to update it somewhere else so that's why we're doing it like this for now so once we have created basically our controller we're going to be creating first our first action right now so how go how what type of action we're going to be creating is going to be a simple get response so first of all we're going to add a route for it tell you what without the route right now we're just going to put http get and basically what we're going to be doing is we'll have the public it's going to be an i action result and basically we're going to just say get and basically here what we're going to be doing is we're going to be returning okay and we're going to be returning hello from the controller and that's it as simple as that so let's run it in our debug mode again let's start it so let's go to our web browser and let's refresh this and we can see here that directly i have my new controller is called teams and i have one api endpoint which is called get and if i click on try it out and we can see here that this is the expected response of a 200 if i click on execute basically my response will be hello from the controller and i got basically the 200 which means that it's working as expected so here you can you can tell that i did not give my uh endpoint a name i just utilized i got and it automatically gave it to it but for example let's say i want to give it a custom name so for example instead of utilizing this endpoint here what i want to do is i want to basically have a route so for example i can put route and i can give it the name for example get best team i don't know anything right now so if i click on stop right now and i click on run again if i go back to my web browser and i refresh this we can see that now my endpoint has a specific name get the best team instead of having it only as gut and why the why do i want to have that so let's say i have an endpoint which has for example 15 different sorry i have a controller which has 15 different endpoint by having the route uh information there it will help me specify which endpoint does what instead of only uh relying on the http verbs which is going to be the post get put patch delete so on so forth this will help me basically understand what what other functionality my api endpoint will provide me so that's why you might want to use that out there in order for you to have to specify or basically help the developer understand what are the uh what's this endpoints gonna do for them so right now we're gonna stop this and we're gonna just delete this because we don't want to use it right now let's keep this so next what i'm going to be doing is we'll start we're going to start utilizing our in-memory database so on top here what i'm going to be doing is i'm going to be creating a variable i'm going to call it or let me make it a bit easier so list of team i'm going to call it teams equal new list of themes and basically we're going to be defining the streams here so first let's fix those references and right now what we're going to be doing is we're only going to be creating two teams so the first team it's going to be of course the country is going to be germany and of course team number one and formula one although not the season yet but it's going to be mercedes so we're going to put the id equal one and the name is going to be mercedes amg f1 and team principle is going to be toto golf i think that's the first team that we want to add and the second team that we're going to be adding is new team and we're gonna basically have the country which is gonna be italy the id equal to the name it's going to be ferrari or scuderia ferrari and the team principle i forgot the other comma says other comma the team principle is gonna be i don't really know how to spell the the principal's name so let's check it out so we're gonna go to google and we're gonna put ferrari f1 team principal bonito but how do you spell and now we have the ferrari and we have the mercedes amg f1 team and basically we're gonna only be utilizing these two teams for now so once we have built this let's make this smaller and now we have this is basically our in-memory database because we can see that this is hard coded to our application there is no like database connections there is no api calls to get this data is simple in memory database let's add one more let's add one more so new team and basically we cannot forget about i think they're in switzerland yes it's alfa romeo so we're gonna put id equal three and then we're gonna put the name alfa romeo and the last one is the principle i don't really know the team principle for alfa romeo so let's check this out frederick bowser so hello frederick perfect so now we have three teams and we know that protest is with alfa romeo and he's doing really well we have we have sir lewis with mercedes which is working on their car and we have uh basically ferrari leading the team this year so just a little fun trivia okay so now that we have this what we're going to be doing is we're just going to be returning this to the user so how are we going to be doing that we're just going to be returning the teams so we're going to put here teams and it's going to be as simple as that so now let's run this again and let's go to our web browser by frederick and let's open this let's refresh this and right now here if i click on try it out and if i click on execute i'm expecting to see a list of all of the themes so if i go here i can see that i received back a response with a list of all of the teams which is mercedes ferrari and alfa romeo and we can see here that basically i'm utilizing my inline memory here and just for me to update it and basically get already not updated just to get all of my information so that's what the first thing that i want and that's the first endpoint that i'm going to be creating so the second endpoint that we're going to be creating is i'm going to be passing in a uh id so for example right now i'm getting all of my users uh sorry all of my teams what i'm gonna be doing next is i wanna just pass an id i'm gonna get the information for only this team so let's see how we can do that so in essence what we're gonna be doing is we need to create a new action so here first let's stop this and basically the first thing is going to be http gut again because we're getting information from the server we're not sending it we're requesting information public by action result and we're gonna put get and we're gonna pass here an end for an id and basically what we're gonna be doing is we're gonna be utilizing link to sort through that list and get the information based on that so we're gonna put for team equal teams dot excuse me dot first or default so if we have it or not and we're gonna put here the condition x dot id equal equal id and then what we're gonna be doing we're gonna say if theme equal equal null what we're gonna be returning is about request and we can say invalid uh invalid id for example we should not really give this detail level of details but for now it should be fine and if everything goes to plan and i the theme actually exists we're gonna just return an okay with the team inside of it okay i think that should be good and now let's stop this and let's run this again okay so this is gonna cause us an error why and that's a very good question because here basically right now okay let's go up a bit let's make this a bit smaller so we can see this we have two http get and right now when swagger try to configure them swagger is telling me okay you have to http gut but i don't really know why there's a duplication there swagger will not really care at this moment in time about this so what we need to do is we need to basically tell swagger that for this http there's going to be an id expected and basically for this the swagger will need to cater for it so let's fix this right now so let's stop this and what we need to do here is after the http get verb we're gonna be basically opening a bracket and basically we're gonna put the curly braces and inside this curly braces we're gonna put id and this id here needs to match exactly what we have here so if we even click on this we're gonna say it's gonna give us a constraint so what does that mean so basically we're telling right now that this http gets uh this http get action is expecting an id of type and which exactly match this one here so let's see this it's gonna run so we're gonna put run this again and let's go to our web browser we see that we don't have an error right now we go to our web browser and just refresh this we can see that we got our endpoint as expected so we can see here that we have two guts but here is expecting an id and we can see here that now right now it's being identified as an integer so if i click on try it out and if i click on one because that's one of the ideas that i have first of all let's double check so try it out execute we have three ids one two three mercedes ferrari and alfa romeo so here for example we're just going to get the mercedes team if i click on execute we're going to get only the mercedes team with all of the information if i click on id for example doesn't exist so let's put the theme i don't know 12 if i click on execute now i should receive an invalid id with a 400 bad request so that's the exact uh outcome that i'm expecting so first of all is i was able to uh check get a valid response based on a valid id and i got a bad request based on inverted id so that's exactly what we want so once we have covered that let's stop this the next step is right now i have gotten the full teams i have gotten a single team the next step is i wanna get i wanna be able to add a team directly from my api and basically the first two i'm getting information this action right now i'm gonna be putting information on posting information to my api so that's why it needs to be an http post so let's do this right now so here we're gonna put http post it's gonna be public i action result of post we're gonna call it it is gonna be expecting a team and we're just gonna call it team what we're going to be doing here is very simple we're going to say var actually it's going to be basically teams dot add theme simple as that and then once we have returned that once you have make sure it's added we're gonna be returning first of all we're gonna be returning a so something we need to be wary of is once we actually post something to an api what we need to receive back is not at 100 we need to receive a http status code of 201 and 201 is basically a http status code which confirms that a record has been added to our database or to our api so that's the first thing that we need to receive which is an http response of 201 the second item that we need to receive is we need to receive a url of how we can get that item itself so how so for example right now we need to receive a url with the http gap with an id in it so we can get this and lastly what we need to receive is the body of that object that we need to add and these are all basically api standard that we need to follow when we are building our api so let's see how we can implement these so it's going to be very simple for us to make this happen so after return what we're going to be doing is we need to put created at action and if we see what's kind of expecting excuse me it's going to expecting the action name that we have in order for us to get this id and then basically from there it's expecting the route value so basically which id we're gonna be passing to that endpoint and last the object value so let's do this right now so it's gonna be get the id or the route value that we're gonna be passing it's gonna be the uh i think we call it team.id and lastly we're gonna be passing the theme that's gonna be the object that we're gonna be sending back very simple dotnet will take care of all of this all we need to utilize is the right one so once we have created this uh let's run this again and let's navigate to our web browser perfect let's refresh this and now we see here that we have a new post endpoint which is available for us and if you click on it we can see what is what is it expecting so it's expecting basically an id the name the country and the team principal name if we go a bit down we can see the response right now it's going to be success and of 200 which is not the case it could be it should be 201 so let's try this out so let's click on try it out and we're going to put an idea three we already have three we're gonna put id number four and which team do we wanna add next so we have already added mercedes we have added let's add mclaren and the country is going to be the united kingdom and the team principal is zach brown so let's execute this and let's see what did we receive so first of all we can see that what we received is we receive that 201 which is exactly what we wanted as well we have received back the object that we have sent which is exactly what we wanted and if we take a look here at the response header we have received the location and basically the location here is telling us from where we can get all of the themes that we have so if we put this like this or basically if we go back up we can see that it's giving us all of the themes that we have here simply to this if you try it out click on execute and we can see that here i only have these why it did not add teams the team let's do this rest of this let's run this let's come here and let's refresh this i'll tell you what before we refresh this so let's copy this so we don't have to do it again let's create a new theme try it out execute one created and if we take a look at the console we see that we have four teams which means that we have added them correctly and if i click on teams try it out got on the receive three themes let's take another look at our list to see what's going on so if we take a look at here oh okay the issue is here because we did not specify as like as a stat the list as static so all we need to do is just make it a static list and that should basically fix our problem so let's stop this let's run this debugger again perfect so now let's go back to our web browser let's refresh this and basically let's add a new team try it out let's put this here execute we can see here that we have that 201 it's added successfully and right now if i come here and click on try it out execute we can see that my team has automatically been added perfect so now that i have all of this in place let me delete the extra let me first minimize this let me remove this we don't need it anymore and right now basically we have three out of uh the four uh end points that we want to create so the last endpoint actually let's create five so another endpoint that we want to create is basically we want so first thing is we got all of our teams the second time uh the second endpoint is we got the single team that we wanted the third one is we were able to add team now the next endpoint is for us to actually to be have the ability to modify a team's information so for example if i want to update the name or i want to update the team principle or i want to update for example their location let's try this out so in order for us to do this we have two types of http verb that we can utilize so we have the http put and we have the http batch so when do we use what and when do we use patch so in essence put is utilized for a full-fledged update of our entire object so let's say we want to update our entire team information we utilize but if we're only going to update a single piece of information let's say we want to update the country of the team so we utilize patch so patch is utilized for partial updates and put is basically if we're going to do a full update of the entire object so for the sake of this video we're going to utilize patch to do some partial update on the country of the team so let's try this right now so here we're going to be doing is we're going to be adding a new http verb http patch and basically it's going to be public by action result it's going be patch and basically it's gonna take a an id which id which team we wanna update to and a string for a country and we're gonna be doing the same thing as we did for the get so first of all we need to find the theme teams dot first or default it's going to be x x dot id equal equal id so that's the first thing we want to do and get the theme we need to check if it's null or not so if team equal equal null we're going to make return request i will say inverted id we should not do this but this is for a demo and lastly if everything is in place team dot country equal country so we're gonna be returning a uh 204 which is no content so in essence whenever we're doing a patch uh we're just gonna return the status http status code 204 instead of returning a 200 because that's the expected one and the body should not contain anything so here all we need to do is all we need to add is a return and it's going to be no content and then simple as that so let's run this and let's open our web browser and let's refresh this page and here we have the patch so for example i'm gonna try it out i'm gonna put i wanna update the id let's see the teams again so i want to update the mercedes team the country from germany to the uk so let's add this so the id here i'm gonna put one and the country i'm gonna put united kingdom and i'm gonna click on execute and if i go here what i have received is basically a 204 and http no content nobody at all so if i go up and i click on execute again i you see here that the united kingdom has been updated from germany so this means that my patch is working as it should and the last one that i want to do is basically the delete so i have get which is going to be as we said before all of them i'm going to get a single team with a gut with an id post which is basically ending a team patch is basically updating teams information and lastly what i want to do is basically delete a theme so let's put a http delete i'm going to put public i action result it's going to be delete and we're just going to take an id for the deletion and basically right now i'm gonna do the same thing so let me copy this instead of having to do it all over again and what i need to do here is i'm just gonna put teams dot remove and i'm going to remove the theme from it so once i removed it the last thing that i want to do is basically i need to return and what i can do is i can return on all content as well and let's try this out so let's run this in debug mode let's open our browser let's refresh this we can see that we have the delete so i'm gonna click on try it out put the id3 execute and we got a 204 so right now if i got all of the lists i only have two teams perfect so now that we have covered the basic card operation which basically get one get all of them add update and delete the next step is uh instead of utilizing an in-memory database we're going to be utilizing ef core in order for us to have an actual database which is going to be a sqlite database where we're starting all of this information creating adb context having a having a migration script there as well linking everything to our controller so let's get started with that the first thing that we need to do is we need to install some packages and we need to install some that nut running to make ourselves uh our life a bit more easier so let's utilize the build in terminal so if we click on terminal here and let's choose let's see where we are okay perfect so let's navigate to our application first let's install the first tooling that we need and this is going to be an enough af core tooling so it's going to be not tool install dash dash global dot nut dash yeah i already have this installed if you don't uh it's gonna take maybe a minute for you to be installed but uh make sure you include yeah you have the global globus it will be available everywhere on all of your projects so once we have that it says that net tooling is already being installed perfect but for you it might be different so uh the next step is we need to install some packages and these new newer packages if you're using rider there's two ways so you can just right click on the application you can click on manage and you get packages and you can choose here from all of these available nuget packages so for example we need to install uh sql lite with entity for entity framework or so we can just type microsoft soft dot entity framework core dot sql lite so we can see here that we have the library available for us and we can click directly on install so we're gonna do one package like this and it's gonna ask me are you sure you wanna install it yes i do and i think now it has been installed successfully so the second way we can do it is we can do it through terminal so in case you're utilizing visual studio code how this is how you do it so first we need to navigate our app and inside here you need to utilize the nut sdk in order for you to basically add this so it's going to be something like this dot not add package and it's going to be a microsoft dot entity frame work work core tools and we can see both of them installed the same at the packages for us it all depends on how you want to do it and if you want to verify that has been installed successfully right click on the name of it on the name of the solution and click edit cs approach and here you can see that we have the two packages available and installed for us and this is exactly what we wanted so once we have added basically our tooling in order for us to basically enable entity framework inside our application and the next step is just let us just build the application uh make sure that still running as it should be and once we do that we're gonna basically update some settings to add the connection string and then we're gonna start building our db context because basically the db context is going to be responsible to all of the communication with the database and this video is going to be utilizing the code first approach so we're going to be creating the models and dedicating the database of that so let's do a build and we can see it's taking a few seconds because it's making sure it's restored all of the packages and now it has built successfully now let us run it it needs my password so let's add the password [Applause] perfect let's open our browser and just refresh making sure that still loading [Applause] again the password oh my god okay uh we can see here that we have it uh ready for us we click on try execute and you can see we're getting the in memory database which is exactly what we want to see okay perfect let us stop this so the next step right now is we need to open our app settings and inside our app settings what we need to do is we need to add basically a connection string and this connection string will tell the application where it can find the database that we are going to be communicating with so we can put it basically everywhere so let's put it here and basically what we need to do is we need to start creating the section for the connection string so it's going to be something like this connection strings and basically here we need to put our default connection you can have multiple connection strings for different items you can have one for the main application one for the logging you can have one for the main application another one for example for third-party services there's multiple ways you can set different types of why you why you would want to have a different connection string for different databases so once we have done that now because we are going to be utilizing sql lite the connection string for sqlite is different than the one for sql server so let's see how that's going to be so it's going to be data source equal we're going to call it app.db and then basically we're going to enable cache for shared okay now once we have created our connection string the next step for us is basically we need to update our program.cs in order for it to actually know that there's going to be a database and there's going to be a connection thing that it needs to be taken into consideration so that's what we need to do here so we need to update this program.cs and we need to configure sqlite with it so because we're utilizing here uh in the previous one we used to do it in the startup.cs previously.6 and we used to do it inside the services so here what we need to do is we need to tap into the build.services method and do it from there so let's put builder dot services dot add db context we don't have a db context yet uh let us create a db context before we can do that i think that's a better idea okay let's create a db context so let's create a new folder new directory we're just gonna call it uh database or we can call it data okay let's call it data and inside this data folder we're going to basically create a new class and we're going to call it tv context simple as that if i can type context and we can call it fdb context better okay uh and basically this one is gonna inherit from the db context so basically because we have added inherited the db context from here it means that this class right now will act as a database db context for our application so once you have get our db contacts here let's go back to our program.cs and let's resume doing this so here we're just going to pass the db context that we have created it's not done by any mean but this is the first step so let's import the missing references and now we need to configure this db context so with we're telling it that it needs to know how to utilize sqlite uh and if you're utilizing your sql server postgres oracle db mysql this is where you need to do the configuration for them so let's add them so we're gonna put options and basically it's gonna be options dot use use sqlite and then here we need to tell it where it's going to find the connection string for that sql light so the first step is that we're basically configuring the options to utilize sql light the next step is returning it where it needs to find the connection string so we need to put configurations or we can put builder dot configuration uh dot get connection string and we need to put that's gonna be the default connection and that should be it for configuring it and right now let's make sure i spelt it correctly default connection and let me just put this here okay perfect now let's just just uh run we're not gonna do anything we're just gonna run it to make sure that everything is still running it's really important for you to keep running every now and then the application just to make sure that you did not introduce any breaking change to your application see so here we have a breaking change so it says add the b context called configuration but type i've only declared as a parameters constructor okay that should be fine because we did not complete the work on the db context yet that's completely fine so let's do this right now let's continue working on our application db context so the first thing we need to do in order for us to start working on our application db context is we need to define our entities so what what i mean by entities here is what's are the table that we're going to be creating based on the models that we have so for example in our application right now we have one table we want to think about it is going to be team so we want to basically tell the big application to be context excuse me that we're going to have a one table and that one table is going to be team and we need to tell it that it's going to be responsible for it so let's add this right now and then we'll initialize it through the constructor and basically that should be it for the application to be contacts so it's going to be public db set we're gonna put team and then what are we gonna call it we're gonna call it teams and we need to put the normal cutters and setters okay so if you don't know what a db set means here basically it's like a let's hover over it and we can see but in essence it means that it's gonna consider it as a table and basically what it's gonna do is it's gonna actually uh build the sql script for the class teams and basically create a table mimicking that that's exactly what it means and basically whenever we're doing any communication with the database it's going to handle handle teams as a table so once we have added teams here now we need to do the constructor and basically we need to do some configuration inside the constructor so we can initialize the db context so it's going to be something like db contacts options and then here we need to put our app db contacts because that's the options that we want to do and here we have to put options and then we need to pass this option to our base constructor so let's just do a quick explanation what is this so this is a constructor which is fine uh and here basically what we're telling it is you need to pull down the configuration options that we have put here so if you can see here these are the options that we have which basically use equal light with the configuration and get connection so we're telling it that they're going to take these options that we have provided in the startup of the application in order for you or for others of this class to know to with with to actually to which database it needs to communicate and do all of the work and basically because we are inheriting from the db contacts we are passing this to the base class which is here so as simple as just we're telling this class that you need to find your configuration that we have provided here and you need to pass it to your base excuse me to your base class uh and you might think to yourself so how this is happening how am i setting something inside my program.cs my application db contact is going to know about it and that's a very valid point so you know the way that this works is we have something called dependency injection and inversion of control that's a completely different topic and that's uh i have a video on it i'll make it here somewhere but basically in essence there's a framework within dot nut which is responsible to finding all of these dependencies and injecting them at runtime so we don't have to worry about that but in essence it's something that you need to know in order for you to basically enhance your skills and not so that's why right now uh we're not gonna dive any more into this but if we run the application right now we should be able to basically run the app without getting that error so let's try that you can see build succeeded and we can see right now it has run successfully we didn't get the error and if we go here to our web browser where is on my browser and if i refresh refresh i can still see my endpoints and if i click on right oh this is the other forecast i'm not i don't care about it if i get all the teams we can see i have all the teams okay great that's exactly the behavior that i wanted okay stop now the next step for us is what we need to do is we need to create some migration so you might ask yourself what is a migration so right now all of the work that we have done is basically on the application side we have created a model we have created an application db context but we didn't we don't have the actual database yet and we don't uh we don't have the ability to basically have tables or basically put information to tables why because what we need to do is basically we need to inform entity framework okay listen you have all these configurations at our application now we need to take control of them and basically create my application for create my database for me you need to create the tables inside the database and you need to basically map them exactly to what we have in the application and this process we need to do ourselves we need to type some commands into our command prompt to our terminal and that commands will basically communicate with the entity framework inside our application for it to do the work so let's do this right now so the first thing we need to do is we need to create a migration script and what is a migration script migration script is basically uh it will allow entity framework to read the application db contacts that we have which is this class here and basically it will scan it for all potential database queries so once it's scanned right here it's going to see it has a db set so entity framework will know that it's going to have to create a database uh so it will have to create a table and basically it will go into the teams class it will check all of the fields it will know that it it will have to create a tables with these fields and then entity framework is going to basically create the sql script for us and we're going to see those so let's create this right now so we're going to utilize the nut ef migrations so dotnet is the normal sdk keyword ef basically we're referring to entity framework migrations we're telling it what do we want to use within the entity frameworks and then we're telling it you need we need to add a new one and we need to give it a name we'll call it initial migration and right now i'm just going to click enter we can see if it has started that should be fine and now it tells me that it's finished completely now if i come here to my application always add and if i look i see i have a new folder which has been created form which is called migrations and if i open it i see that i have this initial migration script so if i click on this what do i see i see basically that it's comparing that first it's going to see that i don't have the database so it will need to create a database for me and second it's going to see that it needs to create a table and this table we can see the name is themes based on the name that are given here so if i called here for example muhammad's teams it's going to basically give it the name muhammad stems then it's going to basically populate the columns we can see the id is of type end so if i go to team here i should see end string string string so let me look so i have an end perfect and then i have a string sing sync exactly and basically we can see here that it has set id primary key and this is something entity framework will take care by itself it's going to see id it's going to see it it's going to make it a primary key and there's something really nice which is provided for us out of the box so we don't have to worry about this so now that i have created a migration script you can see here i still have a database why is that because once i create a migration script i need to basically now execute it and how do you execute this migration script very easy it's going to be not ef database update and click enter we can see when this started doing all of blah and we can see now it has finished successfully we can see how we have the word done this is exactly what we want and if i click here we can see that i have a file here which is called app.db which i didn't have before and this is basically my sqlite database and i don't think i can open this inside uh ryder but what i can do or can i to be honest i never tried this before but a missing driver okay that's not the missing driver test connection it is working so let's try okay got it and here i can see my database main tables perfect i can see that i have my teams and i have my columns here and i can see i have id name country okay great that's a nice feature i didn't know that ryder did that okay anyways so now we can we see that we have our database we have the names we have the country we have the team principle exactly as the mapping that i have in my teams so id name country and team principal all available for me let me remove my face a bit so you can see it let me see how i can remove my face okay this one here i'm going to push myself a bit down here we go [Music] okay so now here you can see that we have id name contributing principle similar to what they mean here okay great so now okay let's see a few files we want to add them we don't want any of this or press it at this one but we don't need the uh we don't need them to be honest add and let's close this okay so now that we have created our database the next step is basically we need to be able to utilize them inside our controller and basically we need to actually whatever changes we want to do we want to make them consistent so not every time we restart our application that the data has to be reset and in order for us to do that what we need to do is we need to go to our controllers inside our team controllers and basically here what we need to do is we need to first inject or application db context into our controller so basically we need to tell the controller that it needs to realize the application db context and once we have done that and we have added we added the reference we can actually utilize the db contacts inside our our actions and once we add them into our actions we can basically have a consistent database as simple as that so let's do this right now so first we need to create a constructor and then what i'm going to do i'm going to put public private static we called it up to be context and we're going to call it underscore context and i'm going to inject this in the constructor so i'm just going to be db context context and then here is going to be underscore context equal context and once we have done that the first thing i'm gonna do is let me close this it's basically i'm gonna utilize now the application db contacts to actually get all of my data instead of actually relying on this so let us for the sake of this let us combine this out uh not sure how you can run this out here okay let us just delete it so all okay let's okay let's delete it and here for example we'll put var themes equal we're gonna utilizing the db context because we are connecting to the database and then we're telling it that we need to connect the teams table and we're just gonna get everything to list we need to tell me everything and you need to get it to me to list and here we are doing it async and this is not uh the proper way because it's returning a task we don't really want to return a task so what i can do is i can await it and in order for me to await it i need to make this an async and if you don't know what an await and async await is in essence when we send the request to the database we don't want this because right now our example is pretty straightforward it's like a one table with a few records in but imagine if you have a database which consists of i don't know a thousand table and you have more than one million people accessing it you don't want every time you do a connection to that database it's gonna go wait for all of the calculation is done all of the data has been like properly uh gotten and done cutting back you don't want the application to wait for it and you don't want this thread or basically this process from your application to be just on hold until this uh this request has been completed because basically you're just first you're blocking resources of your application second you're racing you're wasting it not even locking it and thirdly it's gonna create a bad user experience so the reason why we utilize async await is basically we are telling it this uh call is gonna be asynchronous so we might do a lot of calls asynchronously and basically asynchronous means that we can do them all in the same time and basically once we set the request to the database do your thing whenever you're ready just let me know and i'll pick it up from there and get the information so that's where we are awaiting them so for for example here we telling it that go do your thing get me the themes of list and get get it back to me and then basically put them in the list as we can see here and then we're returning it so that's the first thing now let's go a bit down so now we have added basically the gaps the second here we can see that it's actually uh trying to find one item so let's update this so what we're going to be doing here is we're going to put var team okay let's do this so let's just update this it's going to be await again because it's always better to do it asynchronously we're going to put context dot themes and basically instead of connecting to the actual list that it was here we're basically just connecting to the db contacts team basically inside the database and let's make it async and we'll put this as an async and we'll update this as a task okay great so now that we have this now let's continue to the post again let us do the same thing it's gonna be await underscore context dot themes dot add we're going to make it async and here we're going to have an async task and another item that i want to do here is basically when you're adding something to the database there's going to be a two step the first step is basically you're telling database you want to add it and then you're actually confirming that by telling by telling it to save so it's going to be a wait it's called context let's say it changes async so that's the first step we added and then we're telling it to save these changes so now the for the post now let's update the patch let's make this first as instead of keep on typing it let's copy this let's update this update this and now here what we can do is going to be as simple as await underscore context dot teams first or default get the information here we need to make it async perfect and the last one we need to fix here is basically again await underscore context dot teams perfect and we need to make here async and the way we delete it is going to be the simple as well await underscore context cloud themes dot remove theme and here we need to make it async actually here we don't have to remove is not async but we need to save those changes so we have to await underscore contacts dot save changes async perfect so now that we have done in this few lines of code basically we have upgraded our application from utilizing an in-memory database which is the list that we had into actually utilizing the sql lites database that we have created and basically utilize the application db constants that we had so this is a very very simple way of implementing an application db context a bit of a more advanced way is to actually utilize like a pattern like unit of work pattern which i already covered in a different video i'll link it as well here somewhere if you're really interested but it's something that you uh it's a way better approach but for for getting started i think this is a good way even if you have a small application i think that just should be fine to create it in this manner so now let's try it and see how everything works so let's run it in debug mode coffee time and now let's go to our web browser let's refresh and right now because my database is empty in essence i did not put any information if if i get this i should receive an empty string an empty array let's see what do i get if i go down here we can see it's an empty array which doesn't contain anything and something i would like to point out is because you are utilizing entity framework we can see here that basically this request that we did let me show it basically this one connecting and list async is being converted automatically by entity framework to this selected statement so select id country name team principle from teams sd and this is execute and this translation is happening on the fly we're not doing it we're not controlling it and the framework is looking at the commands that we're giving it's compiling this c sharp code into actual sql script and basically once it's compiling it's executing it isn't that cool great so now that we have done this we can see that we don't have basically anything so let's try posting a new theme so let's try it out i'm gonna give it id1 the name i'm gonna give it mercedes amg f1 country we're gonna put united kingdom team principal toto wolf let's execute what did i get i got a 201 i got an id1 basically what i needed and if i look here i can see that my answer has been converted and basically to an id and a country named team principle with the values exactly how do you expect it to be and if i go here i go all the way up and then execute this again i should now be able to get perfect i'm able to get the first team so right now let's check this one which is going to be the get by id i'm just gonna put an id of one and i should receive the mercedes team great now let's try another one which is basically patching it so i'm gonna try to update the country from united kingdom to uk let's see if that will work execute we can see we've got a tool for no response and now if i try to get this information again let's see what's going to happen so if i come here and click on execute we should see uk instead of united kingdom so we have here a small error let's see a small bug because it's not updating as expected so we'll deal with that right now so let's see what's going on okay so for the patch let's see what's the issue we are getting this with update oh that's the issue we're getting getting it but we're not actually doing any saving so let's save it right now it's going to be await underscore context context dot save changes async so that's what we need to do so let's stop this application run it again coffee time let's go to our web browser click on execute we can see here that it gave us a 204 if i come here to get click on execute we can see it has been updated to uk if i go back up why it's not this okay if i go back up and change it back to united kingdom and i click on execute i come here to click on get we can see it change back okay perfect now it's working and the last one that i want to do is basically deleting this theme so if i come here try it out one just make sure it first still exists so execute we have one theme only so if i come to the delete here and click on delete if i go here we got a 204 so right now if i click get all teams we should not receive any perfect we don't have any team here which is mean it has been deleted great so basically in essence this is going to be the final point of the video we basically converted the application from an in-memory database to a sql-lite database and basically right now what we did is uh we're gonna go do an overview so basically first we created a web application web api within the dot net six then we have created a controller we explained them uh every single action we tested them within an in-memory database and then what we did is we have introduced entity framework core we have basically created an applica a database we did the migration we updated those migrations and then once we have done all of that we basically converted from any memory database to utilizing the sqlite database so i hope this this video has been helpful uh i know we we did not really delve into a lot of the topics like dependency injection and version of control uh for example middlewares we do not under uh we did not delve into these if you're interested please let me know we'll do similar types of video on them please like share and subscribe if you like this video it will really help the channel and any questions please put them in the comments down below have a good day so the first thing we need to do in order for us to start working on our application db context is we need to define our entities so what what i mean by entities here is what's at the table that we're going to be creating based on the models that we have so for example in our application right now we have one table we want to think about it is going to be team so we want to basically tell the big application to be context excuse me that we're going to have a one table and that one table is going to be team and we need to tell it that it's going to be responsible for it so let's add this right now and then we'll initialize it through the constructor and basically that should be it for the application to be contacts so it's going to be public db set we're going to put team and then what what are we gonna call it we're gonna call it themes and we need to put the normal gutters and setters okay so if you don't know what a db set means here basically it's like uh let's hover over it and we can see but in essence it means that it's gonna consider it as a table and basically what it's gonna do is it's gonna actually uh build the sql script for the class themes and basically create a table mimicking that not exactly what it means and basically whenever we're doing any communication with the database it's going to handle the handle teams as a table so once we have added teams here now we need to do the constructor basically we need to do some configuration inside the constructor so we can initialize the db context so it's going to be something like db contacts options and then here we need to put our app db contacts because that's the options that we want to do and here we have to put options and then we need to pass this option to our base constructor so let's just do a quick explanation what is this so this is the constructor which is fine and here basically what we're telling it is you need to pull down the configuration options that we have put here so if you can see here these are the options that we have which basically use equal light with the configuration and get connections so we're telling it that they're going to take these options that we have provided in the startup of the application in order for you or for others of this class to know to with with to actually to which database it needs to communicate and do all the work and basically because we are inheriting from the db contacts we are passing this to the base class which is here so as simple as just we're telling this class that you need to find your configuration that we have provided here and you need to pass it to your base excuse me to your base uh class uh and you might think to yourself so how this is happening how am i setting something inside my program.cs my application db contact is gonna know about it and that's a very valid point so you know the way that this works is we have something called dependency injection and inversion of control that's a completely different topic and that's uh i have a video uh on it i'll make it here somewhere but basically in essence there's a framework within.net which is responsible defining all of these dependencies and injecting them at runtime so we don't have to worry about that but in essence it's something that you need to know in order for you to basically enhance your skills and not so that's why right now uh we're not gonna dive any more into this but if we run the application right now we should be able to basically run the app without getting that error so let's try that you can see build succeeded and we can see right now it has run successfully we didn't get the error and if we go here to our web browser where it's on my blog browser and if i refresh refresh i can still see my endpoints and if i click on right oh this is the other forecast i'm not i don't care about it if i get all the teams we can see i have all the teams okay great that's exactly the behavior that i wanted okay stop now the next step for us is what we need to do is we need to create some migration so you might ask yourself what is a migration so right now all of the work that we have done is basically on the application side we have created a model we have created an application db context but we didn't we don't have the actual database yet and we don't we don't have the ability to basically have tables or basically put information to tables why because what we need to do is basically we need to inform entity framework okay listen you have all these configurations at our application now we need to take control of them and basically create my application for create my database for me you need to create the tables inside the database and you need to basically map them exactly to what we have in the application and this process we need to do ourselves we need to type some commands into our command prompt to our terminal and that commands will basically communicate with the entity framework inside our application for it to do the work so let's do this right now so the first thing we need to do is we need to create a migration script and what is a migration script migration script is basically it will allow entity framework to read the application db contacts that we have which is this class here and basically it will scan it for all potential database queries so once it's scanned right here it's going to see it has a db set so entity framework will know that it's going to have to create a database uh so it's it will have to create a table and basically it will go into the team's class it will check all of the fields it will know that it it will have to create a tables with these fields and then entity framework is going to basically create the sql script for us and we're going to see those so let's create this right now so we're going to utilize the dotnet ef migrations so dotnet is the normal sdk keyword ef basically we're referring to entity framework migrations we're telling it what do we want to use within the entity frameworks and then we're telling it you need we need to add a new one and we need to give it a name we'll call it initial migration and right now i'm just going to click enter we can see if it has started that should be fine and now it tells me that's finished completely now if i come here to my application always add and if i look i see that i have a new folder which has been created from which is called migrations and if i open it i see that i have this initial migration script so if i click on this what do i see i see basically that it's comparing that first it's going to see that i don't have the database so it will need to create a database for me and second it's gonna see that it needs to create a table and this table we can see the name is themes based on the name that are given here so if i called here for example muhammad's teams it's gonna basically give it the name muhammad's teams then it's gonna basically populate the columns we can see the id is of type end so if i go to theme here i should see end string string string so let me look so i have an end perfect and then i have a string string sync exactly and basically we can see here that it has set id primary key and this is something entity framework will take care by itself it's going to see id it's going to see it it's going to make it a primary key and there's something really nice which is provided for us out of the box so we don't have to worry about this so now that i have created a migration script you can see here i still have a database why is that because once i create a migration script i need to basically now execute it and how do you execute this migration script very easy it's going to be not ef database update and click enter we can see when this started doing all of blah blah and we can see now it has finished successfully we can see how we have the word done this is exactly what we want and if i click here we can see that i have a file here which is called app.db which i didn't have before and this is basically my sql lite database and i don't think i can open this inside uh ryder but what i can do or can i to be honest i never tried this before but a missing driver okay that's not the missing driver test connection it is working so let's try okay got it and here i can see my database main tables perfect i can see that i have my teams and i have my columns here and i can see i have id name country okay great that's a nice feature i didn't know that ryder did that okay anyways so now we can we see that we have our database we have the names we have the country we have the team principle exactly as the mapping that i have in my teams so id name country and team principal all available for me let me remove my face a bit so you can see it let me see how i can remove my face okay this one here i'm gonna push myself a bit down here we go okay so now here you can see that we have id name contributing principles similar to what they mean here okay great so now okay let's see a few files do we want to add them we don't want any of these or this one but we don't need the uh we don't need them to be honest add and let's close this okay so now that we have created our database the next step is basically we need to be able to utilize them inside our controller and basically we need to actually whatever changes we want to do we want to make them consistent so not every time we restart our application that the data has to be reset and in order for us to do that what we need to do is we need to go to our controllers inside our team controllers and basically here what we need to do is we need to first inject our application db context into our controller so basically we need to tell the controller that it needs to realize the application db context and once we have done that and we have added we added the reference we can actually utilize the db contacts inside our our actions and once we add them into our actions we can basically have a consistent database as simple as that so let's do this right now so first we need to create a constructor and then what i'm going to do i'm going to put public private static we called it up to be context and we're going to call it underscore context and i'm going to inject this in the constructor so i'm just going to be db context context and then here it's going to be underscore context equal context and once we have done that the first thing i'm gonna do is let me close this it's basically i'm gonna utilize now the application db context to actually get all of my data instead of actually relying on this so let us for the sake of this let us combine this out not sure how you can run this out here okay let us just delete it so all okay let's okay let's delete it and here for example we'll put var themes equal we're gonna utilizing the db contacts because we are connecting to the database and then we're telling it that we need to connect the teams table and we're just gonna get everything to rest we need to tell me everything and you need to get it to me to list and here we are doing it async and this is not uh the proper way because it's determining a task we don't really want to return a task so what i can do is i can await it and in order for me to await it i need to make this an async and if you don't know what an await and async await is in essence when we send the request to the database we don't want this because right now our example is pretty straightforward it's like a one table with a few records in but imagine if you have a database which consists of i don't know a thousand table and you have more than one million people accessing it you don't want every time you do a connection to that database it's gonna go wait for all of the calculations done all of the data has been like properly uh gotten and done cutting back you don't want the application to wait for it and you don't want this thread or basically this process from your application to be just on hold until this uh this request has been completed because basically you're just first you're blocking resources of your application second you're racing you're wasting it not even locking it and thirdly it's going to create a bad user experience so the reason why we utilize async await is basically we are telling it this call is going to be asynchronous so we might do a lot of calls asynchronously and basically asynchronous mean that we can do them all in the same time and basically once we start the request to the database do your thing whenever you're ready just let me know and i'll pick it up from there and get the information so that's where we are awaiting them so for for example here we telling it that go do your thing get me the themes of list and get get it back to me and then basically put them in the list as we can see here and then we're returning it so that's the first thing now let's go a bit down so now we have added basically the gut the second here we can see that it's actually uh trying to find one item so let's update this so what we're going to be doing here is we're going to put var team okay let's do this so let's just update this it's going to be await again because it's always better to do it asynchronously we're going to put context dot themes and basically instead of connecting to the actual list that it was here we're basically just connecting to the db contacts team basically inside the database and let's make it async and we'll put this as an async and we'll update this as a task okay great so now that we have this now let's continue to the post again let us do the same thing it's gonna be await underscore context dot themes dot add we're gonna make it async and here we're gonna have an async task and another item that i wanna do here is basically when you're adding something to the database there's gonna be a two step the first step is basically you're telling database you want to add it and then you're actually confirming the ad by telling by telling it to save so it's going to be await the scroll contacts let's say it changes async so that's the first step we add it and then we're telling it to save these changes so now the for the post now let's update the patch let's make this first as instead of keep on typing it let's copy this let's update this update this and now here what we can do is going to be as simple as await underscore context dot teams first or default get the information here we need to make it async perfect and the last one we need to fix here is basically again await underscore context dot teams perfect and we need to make here async and the way we delete it is going to be the simple as well await underscore context.themes dot remove theme and here we need to make it async actually here we don't have to remove is not async but we need to save those changes so we have to await underscore contacts.save changes async perfect so now that we have done in this few lines of code basically we have upgraded our application from utilizing an in-memory database which is the list that we had into actually utilizing the sqlites database that we have created and basically utilized the application db constants that we had so this is a very very simple way of implementing an application db context a bit of a more advanced way is to actually utilize like a pattern like unit of work pattern which i already covered in a different video i'll link it as well here somewhere if you're really interested but it's something that you uh it's a way better approach but for for getting started i think this is a good way even if you have a small application i think that's just should be fine to create it in this manner so now let's try it and see how everything works so i'll just run it in debug mode coffee time and now let's go to our web browser let's refresh and right now because my database is empty in essence i did not put any information if and if i get this i should receive an empty string an empty array let's see what do i get if i go down here we can see it's an empty array which doesn't contain anything and something i would like to point out is because you are utilizing entity framework we can see here that basically this request that we did let me show it basically this one connecting and list async is being converted automatically by entity framework to this selected statement so select id country name team principle from teams sd and this is execute and this translation is happening on the fly we're not doing it we're not controlling it and the framework is looking at the commands that we're giving it's compiling this c sharp code into actual sql script and basically once it's compiling it's executing it isn't that cool great so now that we have done this we can see that we don't have basically anything so let's try posting a new theme so let's try it out i'm gonna give it id1 uh the name i'm gonna give it mercedes amg f1 country we're gonna put united kingdom team principal toto wolf let's execute what did i get i got a 201 i got an id1 basically what i needed and if i look here i can see that my answer has been converted and basically to an id and a country named team principle with the values exactly how do you expect it to be and if i go here i go all the way up and then execute this again i should now be able to get perfect i'm able to get the first team so right now let's check this one which is gonna be the get by id i'm just gonna put an id of one and i should receive the mercedes theme great now let's try another one which is basically patching it so i'm gonna try to update the country from united kingdom to uk let's see if that will work execute we can see we got the 204 no response and now if i try to get this information again let's see what's going to happen so if i come here and click on execute we should see uk instead of united kingdom so we have here a small error let's see a small vlog because it's not updating as expected so we'll deal with that right now so let's see what's going on okay so for the patch let's see what's the issue we are getting this we'll update oh that's the issue we're getting getting it but we're not actually doing any saving so let's save it right now it's going to be await underscore context context dot save changes async so that's what we need to do so let's stop this application run it again coffee time go to our web browser click on execute we can see here that it gave us a 204 if i come here to get click on execute we can see it has been updated to uk if i go back up why isn't this okay if i go back up and change it back to united kingdom and i click on execute i come here to click on get we can see it change back okay perfect now it's working and the last one that i want to do is basically deleting this theme so if i come here try it out one let's make sure first it still exists so execute we have one team only so if i come to the delete here and click on delete if i go here we got a 204 so right now if i click get all teams we should not receive any perfect we don't have any team here which is mean it has been deleted great so basically in essence this is going to be the final point of the video we basically converted the application from an in-memory database to a sqlite database and basically right now what we did is uh we're gonna go do an overview so basically first we created a web application web api within the dot net six then we have created the controller we explained them every single action we tested them within an in-memory database and then what we did is we have introduced entity framework core we have basically created an application a database we did the migration we updated those migrations and then once we have done all of that we basically converted from any memory database to utilizing the sqlite database so i hope this this video has been helpful i know we we did not really delve into a lot of the topics like dependency injection and version of control uh for example middlewares we do not under we did not delve into these if you're interested please let me know we'll do similar types of video on them please like share and subscribe if you like this video it will really help the channel and any questions please put them in the comments down below have a good day
Info
Channel: Mohamad Lawand
Views: 9,422
Rating: undefined out of 5
Keywords: .net, api, .net 6, beginner guide, step by step, csharp, c#, dotnet, dependency injection, code with me, coding, ef core, sqlite, database normalization, database, entity framework c#, entity framework core, entity framework, asp.net core tutorial, asp.net mvc tutorial, net core mvc
Id: wtFs4356xp4
Channel Id: undefined
Length: 103min 32sec (6212 seconds)
Published: Fri Jun 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.