Build REST API with C# and Entity Framework Core Database First Approach

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to this channel well today i have something exciting for you guys i will teach you how to create a web api using asp.net core and entity framework core database first approach so you have a database already set up but you just want to use the database to you know create an api for your customers or your users to integrate and to consume and use um so this is going to be the first part the first series i will be teaching you how to create the api on this particular video uh my second video which i think will be coming out in about a week weeks week from now will be how to consume that this same api that i'll be creating i'll be teaching you how to create it so this video is aimed at or targeted at beginners people that are trying to learn how to create apis using asp.net um um core and cshop now you don't really need to know too much uh you can follow along it's gonna be very easy video to follow along and yeah let's have fun let's create something and let's have fun so i'm gonna also teach you how to use postman to um check the results of your api so let's start a brand new project what's what what should i even call this project let's see let's see what i'm going to call this project let's call it ah let's create a new project okay so i want to select make sure you select asp.net core web application make sure you do that and make sure it's also c sharp this one version for vb.net vb make sure it's that and then you select that and click next um i want to create um api i'm sorry api tutorials api tutorials um yeah that's it that's a good name for it i think just for the tutorials right let's create that so it gives you these options i want to take the api template um that's the one i'm looking for okay so we have a brand new um slate right here the first thing you're going to see is your controllers right now they have a default one which has gives you the water with a weather focus controller and this all this shows just a return to data for weather focus and that's all and they have the weather focus class right here i'm not going to delete it we're just going to leave it there you know just in case you see it in your own project you can ask you know you could see that it's just a default it comes along with it and for asp.net core has changed web web config now to web settings so everything that you used to have in web config now we don't have web config now anymore so we have that in the web settings and this will i mean we have that in the web conf app settings i'm sorry up settings that everything now is in the app settings so you will have to do your settings that you usually do in a web config in app settings so that's that i thought i should mention that and also there is a startup this this is one of the most important files this is what actually loads your whole um web apis startup here you can configure um everything and then this program dot cs file it's what loads it it's like it's like remember when you do console application yeah the static main void it loads it from here and then it calls that startup file right there it calls the startup file and then instead of file does all the bootstrapping and all those things to start up your website to set up your api so this file is very important uh you could come here to do a lot of setup here like um this is where we're going to register our db context and connection string we're going to register them here and then it's going to configure it here and then use it okay so that's the file i just thought i should mention that um so everything's so let's let's let's just go ahead and start i want to add a new controller and i'm going to add a controller called users because i have a database already created called uh i think demodb and i have a table there called users so i just want to have a user's controller to show all the users and so basically we're going to get users we're going to create a new user we're going to update a user and then we'll delete the user these four operations will be done in this tutorial that i'm trying to get you guys to watch all right so okay when you come here by default don't make mistake here because it's a lot of times people can make mistake here when you select the add controller by default it is on mvc see this mvc right here by default it's going to see you're going to see mvc right like that right don't fall for it don't fall for it guys you have to make sure you select api okay and then api controller mt1 i'm going to add that and i just want to call it users make sure you don't delete the controller at the end right just delete this and then just add your own user right and then click ok to enter all right so this is our this is our controller just trying to reduce this so you can see right very well um so this is our controller it's this is a route of your controller so it's going to be usually this is a local hose and your run is going to be on your localhost right it's going to be localhost api controller name so that means it's going to be slash user slash our method name right so in this case let's just start let's just build the signature of what we're trying to do so you could see right the first thing we're going to do is we're going to get users okay the second thing will be create a user the third thing will be update a user and the fourth thing will be delete a user so let's start up right i'm just going to do a skeleton and then you see how it works then we can go ahead to set up the database and i'll show you how to save data to database and retrieve data from database and post create a new user in the database and delete to the from database as well but let's just start with skeleton the first thing i want to do is the get so with apis there is um something called the vpn http verbs right these verbs allow you to demonstrate what action you're about to take so in this case we are about to take an action of get right so this is going to be an http get request okay somebody is trying to call you api somebody someone is trying to request an information from your api in this api where they're going to create somebody's going to request an api for us users ap this this api is just users something like that someone's going to request how do we tell our program or application to know what kind of request is coming so we have to set it here so that it will know what kind of request is coming and if the person doesn't follow this request pattern we are not going to show them anything you know so the first thing is the http get http get right right there in this http get what is the name of the endpoint that you're trying to that you want people to hit when you're trying to get you'll give it any name get my friends list get my get products list or get whatever you're trying to hit in this case i'm trying to get users so you put it inside there and then you can do get users like that right so that is get users right and then inside here now we now we're going to hide our function now public i action results oh i'm sorry it's not supposed to be together i action results like that get now this name that i'm putting here doesn't really matter okay what really matters is the endpoint name to get users this is the endpoint name this is what really matters okay um so this is this whatever we return here it's what the user would get okay for now let's just return and okay so okay means everything is correct it's going to be an okay result or okay everything is correct right it's going to return a status quo of 200. if you don't know what the status code is just go to google and type http http space status code you're going to see some microsoft is going to list that or the status code is what all the statistics are i'm sorry um so the okay is a 200 status code it means everything is successful all right for now let's just return that soda so the application is happy you know we don't want to make it look like it's complaining all right the next one is http post this is used to create to create or post an object if you want to try to save something to database or save something to an endpoint or anything use the http post and this is the next thing that we're going to be doing is the same thing you want the end point to be what you want it to be the name that you want it to be you know specific in this case i'm going to just write create user because we'll be creating a user and everything is just the same afterwards so let's just i'm just going to copy this one here over here and i'll put it here but yeah we're just going to create it just to create instead of instead of um get the next one is http put for updating a user to update if you're trying to update something in an api in a restful api you're trying to update something by the way this is a restful app i didn't mention that this is a restful api so i didn't mention that i'm sorry this is a restful api there is restful and then there is a soap api but this is restful http put the http port is used for update to update something or to edit something and use the http put all right so that's that now the next one is delete and as you can guess it is http delete just the same thing i just want to make sure that i'm getting all of this scaling like the blueprints out sorry http delete right there it's coming right there change it to http delete and then this is gonna be delete user delete user okay so this here is going to be delete all right now i've shown you this kind of like the skeleton of everything we are going to do okay if you're just trying to do a get all you need to do is just do a get request and you just do a get and everything will be will be outputted out directly now if you're trying to do a create create request http post you will need to have an object passed along that is what is called request object pass along that the user the person consuming your api will create that object for you and then post it along and then you will take that object and then you save it in database in this case right now what i've done right now there is no object there's nothing it's just the scaling thing what i want us to do is let us go ahead and create that object and pass in the request here so that the person consuming this will know that we require an object from them if they don't pass the object it's going to fail so this is how we create the user right so let's go ahead and create that object i'm going to add a new um a new folder right here just call it models so models folder right and then i'm going to call sorry to keep closing this i'm going to call that add a class and just call it user request user request objects okay user request all right you got that going so i want this this to be similar to what we have in a database and this is a database that i've already created i have these three three records already in here and i have like a username first name last name city state country and the table name is tbl users so i want it to be exactly like this so that when the person requesting um requesting to use our endpoint they will have to pass this value this um properties over and then we can take it and just manipulate and then save the database and create that new user so i want it to be like that and let's go back here and okay i already have it typed typed out in a notepad or not file a file so that we don't have to type everything all over again okay so this is that i will leave it like this you just go ahead and type you can pause the video right here and type along like this okay once you're done you can continue the video and then we can go ahead by the way i recreated a table so you should create a table i will talk about the database part of things but i'll recreate it a table right now i'm not trying to do the database connection stuff yet i just want to show you the basic skeleton or the basic ways of writing the apis how to set it up first okay then once we set it up then we can now go to database and start retrieving stuff from database right so that is that and we should now i want to use the request but i want to make sure that they know that they're supposed to use a request body right so this will be coming the end user will be passing this in there in the body of their request so we need to tell them that we want to take this once we need to tell our application i want to take this from from body this is very important so from the body we we are expecting we are expecting um this request object from the body and they should pass us this request body i'm just going to put username namespace um the namespace bring the namespace let's paste it in all right so they should be giving us this request body over all right and then that now this is this is the complete it's complete right now we we will do we'll do some some things like programming and all of that but i just wanted to set that up well the same thing with update that we update they need also need to pass us a request body so i'm going to copy this as well in here as well well the delete is different delete is they don't need to pass through the request body they just need to pass the id that you're trying to delete in your database you're going to have like an id right you see this id one two three they need to pass us just the id and then we can delete but how how they're going to pass us the id and how they're going to pass the id all we need to do to let them know that they have to pass the id is we're going to put a slash here and then we're going to put a curly braces as a placeholder to tell them that hey you're going to pass us an id and then once the id is passed we're going to tell our method that hey you are expected to take an id now your method now knows that it's supposed to take an id right this is how basic basic layout this is the basic layout okay of all the four http verbs right it's like this cool now let's before i start working on the database part of things i want to just um do a quick get to see that our get is working because what if we spend all our time writing database code and our gate is not working our endpoint is not working i don't know that we need to just quickly test it to make sure that the end point is working correctly for that i will just quickly create a new um let's just quickly create um more like a list of users quickly right here and just try to call it you know so um what if we do something like private sorry private lists of by the way this is just a dummy right don't don't pay no mind to this um i want to do something like um what if i just use the request request object request user request object camel casing that doesn't take any parameter i'm just going to return this but this is just this is just for testing no don't don't do this just watch or if you want to do it you can do it but this is just to let's just test to make sure that our endpoint is correct so that we know that endpoint is correct right new i'm going to do like just username equals to a b c and then first name is first name is john i'm missing an equal so that's why it's complaining i'm just going to put that equal to there all right so how about to just repeat this over and over and over again sorry no i'm just gonna copy that like two two or three times um cdf as a username mary xyz i don't know what a name who has the number that that's that well i just so right now let's just do this and let's just do a get here do this guy for user or users with the s right so let's just do that and they just return this user this is just for tests i'm not trying to be too much fans just let's just just to be sure that our application is working and the first i don't know why for the first time i always run this always it always doesn't hit this place i don't know why but let's see if he hits it this time around for the first time right now it's just gonna hit the first um that weather weather forecast api that's the default one that came with it i've i didn't want to delete it because i just wanted you guys to know that there's a default one there so our on is going to be api slash user slash get users fingers crossed it hits it the first time it always happened to me that every time i build a new project the first time it doesn't hit my controller it does hit it the first time that's good all right so um let's just get the users it gets the user two right that count of three right and then we have it right there um so you see that the user abc john all the other things that i didn't put there is no but this is just to test it and you know just to show that it's the api is working which is good uh what we can do right now is we could do use an application that formats um json very clearly and better and i've actually downloaded um i've actually downloaded postman if you've not downloaded postman you could go to you could just go online on google and just type download post man post man and then you will see it right there and you can download it and then you can use it too i've already set up all these things a little bit so that we don't have to worry too much so i'm just going to change the the port number so that when we when we start using them we just have port number is already changed and then we can just use it all right so so let's do a get so this these ones are the stuff in database i've already been like kind of testing so i kind of have all these guys in database already um so let's do a get use this for the ones that we just did right now so we can see that it's actually working and yeah it comes here still and i'm just gonna take off the break point we already know that it's working and it's hitting the end point correctly and then it returns back our three um so abc john it's just those two i just put the merry and then xyz canola so that means our endpoint is working our api is working now that our api is working with just regular dummy data let us go and learn how to use entity framework core to get data from our database or to save data from our database now let's go and learn how to work with the database now so that we can have a full complete application that is actually saving in the database so let's go to the database part all right let's go okay so i'm going to stop this now and um i'm going to remove this because we okay i could leave it let's just leave it it's fine but i don't want actually let's remove it i don't want to use the requests object to be doing all of that get that's not the purpose of a request object and this gun should be failing right now so let's remove it too and let's just remove this guy here all right so everything is fine now everything should be fine okay now the first thing that we need to do is to install entity framework core and entity framework dot sql all right we could right click on your project and go to nougat packet money manage nuget packages and go to browse and browse entity framework entity framework is going to be out there we're going to be installing two things we're going to be installing the entity framework and enter the framework on entry from a sql server so the first one is this guy install entity framework core okay yes that's nice okay that's nice the second one is entity framework called sql right here dot sql server okay you could see how many people are already downloading 137 million it is used a lot all right so that's that and i'm going to install that one too those are two packages i'm installing and you can install it as well if you're following along all right so i got that installed and you could close this once it is installed so that's the first thing now the second thing is i want to show you the database that i've created already so i have this db demo database right here okay and i just added just one cl one table because it's just one table we're trying to manipulate i'm not good you're not trying to do too much of things just one table insert in displays edit delete retrieve or get from this table right you could go ahead and create this table as well if you don't know how to create a database you when when you go to your um your studio management studio sql server management studio you could right click here add new database like that it's going to tell you database name you added a database name but i already have it so i don't want to add anything and then once you add the database you can add new table no sorry right click sorry right click on table you can add a new table like that and you add a new table and then you just do the same thing that i'm doing here you see username like let me show you my columns just so you know my columns are an id it's a primary key not null username is a voucher 50. first name is virtual hundred and and all of that so if you want to copy along you can just do the same thing and just follow along and just create your table which is nice all right so that's that i just wanted to show you the database the next thing we're going to do is to set up the classes that will help us to retrieve or or query the database there are there is one very important class that you need to set up that in class is the db context class okay the db context class will inherit from db context and then you could set up your dbset class okay i want to first of all set up my db db set class okay this db set class is just the same name as the more like the class for the for the table that will have so this database have a tbl my database have a tbl tbl users right so i want to set up that class here in my database um i'm sorry in my in my c sharps but i just want to kind of differentiate it a little bit i'm going to add a new folder called data so that's going to be like a data full data folder okay so inside here i'm going to add a class that will represent my table like it's more like a reservation on my table and that is going to be tbl user simple right i'm going to add that here and i also want to okay now that has been said i want to remember it's a representation of my table right so the data will be more like what is in a table so in here i already have it typed and kept in a file so in here i'm going to add this file everything is the same as it is in the table a tbl table and remember the id is a primary key so that's why i'm decorating with a key if you have this error that is showing here you right click it and you select this is trying to just use the component company model.annotations and you select that yeah so this key now is telling of c that hey this is a primary key on this table class all right so he knows that the next thing we are going to do is to add a db context class okay and then this db context class is going to go this in the same place as our i'm going to add a class so the next thing is the db context class and this is for me i'm just going to call it the um so if you look at if you look at my db my database right i just call it db demo db right it's a demo database right so i'm trying to have the same naming convention since my database name is demo i'm going to call it demodb context right so it's more like the database name and the context of the database name right so i'm going to call it demo db context right save that well in here this class should inherit from db context okay from from the db context class so have to import the namespace here we have to bring in the namespace using micros that's why you see why we installed the entity framework core because it needs it right here so now i'm going to add a using statement import it in there so it's going to add it up here so that's why we needed that right so in the constructor of this class we have to do some setup so just do your ctor if you do a c2 like that and hit tab tab two times just hit tab two times it's going to generate a constructor for you which is cool because i don't want to be like typing all of that okay so now we have the db context option we have to the db context option right here type your db context option and after this that every day everything is going to be like then you have to pass in your demo demo db like your demo your class name i mean pass your class name context like that and just give it a variable in this case i like to put it like options right and this class will inherit from the base um like that and also just pass your options very very what i created there all right so that's that's it for setting up this class but we need to um we need to tell the db context so from the db context we're supposed to be getting the tables right but what table are you trying to get in this case we have one table if you have multiple tables you're going to put it here so in this table in this db context we want to get to the db set and that db set is our table that we just created a class table remember that class table user and this is where tbl uses okay and that's we do have a get and we do our set right that's it okay oh sorry this set set needs to semicolon so that's it and then we're almost set to go we're almost set to start querying our database right we're almost reset okay start creating the database um the next thing we need to do is to go to the startup file right in the startup file we need to tell the configure services in a configure services method we need to tell the application that when it's starting up you should load our db context model and load it with our connection string so that you can use the connection string to get the data from our database so that being said we need to implement our connection string or write our connection string usually before now before the entity and before the asp.net core the connection strings we have been written in the web config but now we don't have web config we have app settings.json so this app settings are js in this one right here this one so right here we're going to write our connection string right here and again for me i already have it written but i'm going to write it just so you know how i already have it written in a in a word document in a text document but i'm going to write it just so you know so that's connection string right there and then the whatever name you give here is going to be the name of your connection string for me i just gave it default connection string and this is where i'm going to copy what i've already have over to save over time and i'm going to explain it after i copied over all right so this is the name of my connection string default connection string my data source is localhost now be careful with the data source be careful because this might cause a lot of errors for you when you when you are running your code on your own and all of that now this data source is the name of your um instance name so you see you see how my my um management studio the local host is right there this is my data source if you want to know your data source you can just click this connect button disconnect right here you see how my own has localhost your own might have your computer name sql server express it might have something else so be careful make sure it is the same as as it is here or or the same as it is in here okay all right um that being said initial catalog is your database name my own is the demo db you could see it right there is demodb um then everything else is pretty much um basic um integrated security true and this one is true this is because it is for local connection you know that's why we just put integrated securities through there be careful about this connection string and your data source look do the source it used to cause a lot of errors for people that don't know what that data source is or instance name is for me it is local for you it might be different for me it's localhost for you it might be different so be careful verify on that one before you go ahead initial your initial catalog is your database name and then just set this as it is like this right now integrated security true multiple active results set true leave it like that and everything should be good default connection is my it's my um connection name so let's go now now that we have our our connection string in its app settings save it and let's go to the startup this startup is where your application will kind of load everything that is needed to connect to connect to different things like database and something something's going to load it here and then get you up and running got your application up and running well in our own case we need to connect we need to get our that kind of the connection string so i'm going to do connection string connection string i like to type it all out just so people are watching just so they can they can like know and then this is camel casing as well so let's do that string yes so if you look at the configuration this configuration here you can use this configuration to get your your app settings stuff in your app settings so configuration configuration dot gets connection string get connection string it's right there and this one takes our default setting well it's the name of your connection string for me again i re called it default setting default connection and i like to copy it over just so you don't make mistake i just like to copy it over like that because if i type it i might make mistake okay now that we have our connection stream we need to tie this connection string we need to pass this connection string to a db context so the db context can use it to get to the database and then this is to do that you use the services sorry there's no services services that add db context rdb context demo okay i need to make sure i type it correctly demo db context so now we're telling it that we're adding our db context here so if our db context was demo db context i want to make sure that i bring it bring it in because it doesn't know using the namespace so you bring it in like that and now it has a namespace in um options going to options options dot dot use um use sql use sql hold on it doesn't look like this has the the namespace here so i have to type it manually and make sure that i type it correctly and then i can bring the name space over if you to let me yes bring the next piece over so it's using entity framework core so that's why we installed entity framework at a time right and installed it doesn't look like it it doesn't look like that's it but that should be it right well it's looking for the connection string right now so let's pass the connection string and i think it should be okay what is this quit helping me all right so i'm going to pass the connection string right there and once we put our semicolon everything should be fine okay in a nutshell this is all we need to do to connect to the database in a nut i'm going to summarize the first thing i did was to add a class remember this class if you add another a different table to your database you need to add that class that table class here again so for every table you add you need to be adding the classes right here so for me i just have one table and then you need to add a db context class once you do this setup you need to if you have if you add another table you need to do this dbs db set again for that table if you add 10 tables you're going to have 10 db sets here and that's once that is done you probably need to go to your connection string and set up your connection string like that once that is done you come to your startup class in the configuration configure services method you set up your db like db context like that passing it to the connection string like that and that's it that's what we need to set up to communicate with a database now that we'll have database set up this would be a good time to actually go and test if our database is working to just retrieve the users remember we have three users in this table we have test one test two and demo demo user one we need to be able to retrieve all of those from this database using our newly set up database so let's go and do that so i'm gonna start start doing it but before we do that we need to get access to our database here access to what we need to get access to db our db context database right here so i'm going to do a private demo db context it's not the namespace is not here so i need to type it out maybe context um i like to call that just underscore db context maybe context right well we need to bring in a namespace so when you hover over that little yellow bulb will be out so you right click that the namespace is there you selected and that's good and in the constructor i just want to do um a little bit of injection in it so you do see tour okay and i want to inject this over there in the constructor and then just assign this guy over to this guy that's that should be it oh no what did i do all right that's that should be okay db context all right so that's it now we have access to that and we should be getting it now here but i want to do a try cache just in case there's an error that comes in if there's an error we need to throw a status code that there's an error if you look at the status code if you go look at your status code again i told you earlier on in in the video to go look at the status code if you look at your status code again you will see that um you will see that um there are status code error 500 is for internal server error so if there is for any reason we have some error just happened while we're trying to connect to database we should throw a 500 arrow internal server error something as a code so an error is a code or internal server error as a code or something like that to tell the user that something happened you know so we could return this error so you could type you could type return status code so we should see it right there status code right here right status code and then if you do 500 if you do 500 status code 500 like that okay and then you can actually pass an object value just in case your company needs a particular like your company has like a particular arrow that is being set up or also you could actually um set it up with this and for here i'm just going to say an error has happened it's going to say an error as a code like that would do for us and then for our to get the data from our database we need to do the db context so it's var users equals to a db context that we created right here dot tbl table remember this table name that we created right here yes so now we're going to use it table this is taking this guy right here db context is getting this guy and this tbl uses is getting the the list of users and then we are doing to list now if this this list doesn't return anything we also need to throw an error that the user is not found so we're gonna do it if if users.count users.count is equal to zero that means we don't we didn't find anything we want to throw an error user is not found so that is a 404 for status code status code 404 means the whatever you're looking for is not found so it's not available it's not found so status code 404 no user found okay but if the user is found they want to return an okay result with the user data so we want to return an okay result with this user data right here if the user is found okay so this this okay result here at the bottom is not needed make sure you remove it okay make sure don't forget to remove this guy if if you if you're returning your your user right here make sure you remove this last guy here don't forget that okay so that being said uh so i think we are ready to test this again to make sure this is coming from database well if it fails then we're going to fix some things so well it's good that we're now gonna try from database okay so i'm gonna run this again and once it's running once it's loaded i'm going to go to a postman and i have a breakpoint right there go to that breakpoint should um let us test it very well test it okay so it's loaded so let's go to postman remember we did the test with that dummy data that was returning a lot of knowledge now now we're actually going to database so let's hit it from the database and make sure it's hitting our database okay so it comes here so now let's see if our database will fail or not connection will fail oh where connection did not fail and we should have a three three records back yes we got all of them back and it's the same thing as it is in my database so if you look at what is in the database test one test two and this guy so it's the same thing so it's kind of hitting our database and bringing our record back yes so we have our record back right here test 1 test 2 and test demo user one well if you're able to get to this point congratulations you've actually been able to successfully create your api using entity framework core database first approach it is it looks it is not that complicated but you might have some errors with entity framework if this setup is not correct so that is that now let's go and do the same things for like let's complete the the rest of them for the to create a new user and update user and delete user and then we can run everything again and test everything together once we do this okay so i'm going to stop this i'm going to start it again and go to the create user portion of it okay so all right so that being said for the create user portion of it we need to create a new instance of this class remember this class that is for like the table the tbl user that is more like the representation of a table we need to create a new instance of this class and then assign the request assign the request values to that class and then pass it to the database context to the db context and then save the db context changes db context so again let me recap that we need to create a new instance of the class tbl user and then assign every value that we have from here to that class to that instance and then save it to the database using the db context okay so to save our time i have already typed it out so it's going to be something like that okay it's going to be something like that we're creating a new instance of that tbl user class which is like the tba user table and then for every request for all the things in the request we're just passing it over to here pass it over to here pass it over to here now this tbr user table instance class now has it okay now we need to save it to the database so we need to pass it to the database table and to do that i want to put a try cache now because they said this is a database stuff and chances that it might fail right so again if it fails we're going to return a 500 status code that it is it has failed so there's a 500 error status code something an error as a code if it doesn't fail again let's remove this guy before we forget it's nice to always remove because we're going to return it return return the whatever we save right there or we're going to get new users and just return if it doesn't fail we want to add that to the table so if it doesn't fail we want to add it to the table so we're going to add it to the table right here okay remember is the db context dot tbl users so the db context remember this guy right here this is the db context there and we are passing it into this table right here into the db set the db set table db set right and then we add it okay now once it's out now we need to save changes now that save changes will commit it to database and save it to database okay okay now this is where the error might likely occur if there's an error if your code doesn't break here then your save is correct okay but if it breaks here it's gonna throw an exception here and you're gonna catch the exception right here if you want to see what the exception is make sure to put an ex here i don't like yeah just put an ex here so you can see the exception so when it breaks it through the section you can inspect this ex to read read what the error message is and then you can catch where the exception is from there okay so that's that now wouldn't it be nice after you save it we just retrieve we just retrieve the data from the table that has been saved so we could see that what we saved actually was returned back to us so we can get the full data that was returned and that's what i did um and then i'm gonna just copy that and paste there because i already wrote it on a um text file so you're gonna do like a get again like remember how you did it get in the first time in the first place remember how you did is get this get right here we're just going to do it again and just return return that users back and then this time around you return to users as that was saved as well okay so that's that now this is all done for i have to put a breakpoint here so i won't test it we can hit the breakpoint just so we can see it's that it's hitting here i'm going to remove this breaker i've already tested this one so let's go for update user section the update user section is almost the same thing as a create but the thing is with the update we need to we need to make use of the id of that user that we're about to create update i mean we're about to update because we need to query the database table to get that user and then make changes to that user's role so that's why that's one of the only difference that with the create and of course i already explained that update is http put so for that i'm gonna again i'm gonna remove this this guy just so because we're writing this guy so i'm gonna start with the try cache again just in case there's an error i'm just gonna catch it oh i'm sorry i'm sorry i didn't mean to do that okay ex okay and like i said we're gonna we're gonna do um get user sorry let's step that var user equals to db context just in case you're confused where i'm getting this db context if you're confused remember that i created this private db context at the start of the page when we were doing when we started doing the db db stuff and this is the b contest i'm getting and okay i'm using just in case you're confused all right so that is that so what i'm trying to do i'm trying to get the tbl user table tbl user where are you right where cb is a tbl users table dot i want to get it first or default so first one or return the defaults and lambda expression where x is going to i'm sorry what am i doing x is going to x dot i'm looking for the id in this table and where the id is equals to our request object body request object from the body dot the id portion of it so somebody the person the people requesting for this this data will will be passing us an id right so we want to make sure that that user is existing in the database before we go ahead and make a change if not we want to try an error known that it's the user is not found something's wrong so we're going to make sure if the user if user is equals to null if this is equal to null let's just draw an error and tell them well hey something is wrong user is not found there's a 404 error for if i i've mentioned if about four for status error code user is not found 404 is user it's not it's not found your date your record is not found something's not found your what you're trying to request for is not found okay so that is that and then if an error happens remember how we always throw an error if an error it's like if exception happens we want to throw a status code 500 which is like an entire internal server error or something or something something went wrong so we just want to put it right here so just so we are fully making sure we are actually catching all these little cases you know because it's possible that some things could go wrong in your api and you need to make sure you're catching all these things and you're returning the appropriate errors for them and the error code for them as well okay so now that we make sure that the user is found and when we make sure that that user is found and it's not null then we can do the mapping now again again now we're going to be mapping from whatever value that we have from the request we're going to map it to this user so we're going to be changing the user data by passing these values from the request into the user data right here and then we're going to do the same thing as to save it so we're going to do like that so from the request from the request that is coming from the user we are passing username back to this user so if if the username changes from request it's going to change in this user as well first name whatever changes from this request is going to change here too and then when that is done we are going to do the dbcontext.entry don't forget to do dbcontext.entry dots so you do that db context that our same db context dot entry you pass the entry which is the username it means the user user object is the entry dot state equals to entitystate.modified that means you're modifying that record well in this case this guy needs to get the entity framework call using namespace so just right click and add it or if you're typing it if it if you're typing it and it's not there just type it first make sure you type the case in correctly uppercase lowercase correctly and then just right click and then add it okay and then once you do that don't forget to save your changes so the db context or save changes is the next line that we're going to do okay so once you do that um db db context with save the changes and then your user would i would would have been updated by now but again i'd like to see after our debut user i would like to retrieve the the table back just to see what the changes are so that's why i'm going to be adding this this line to get to retrieve the user as well remember what we did when we were creating again when we updated we just retrieve it again db context and tbr user retrieve it and then return now okay okay is a status code of 200 200 status code means everything was successful and okay means everything was successful and they will pass will pass in this users that we are retrieving from database and it's going to launch it out so that is that that was fun right that was fun well you can pause the video and type along and then you could pause it right here and type all these things once you finish typing this top part i'm going to scroll down and then you're going to type this bottom part for that okay once you're done with this part i want to go to delete so the delete is it's kind of simple because we're just trying to get um we're just trying to get that particular use and just delete it that's simple right again i want to do the try catch because i'm hitting database oh no no i always do this making sure that that's not selected before i make my next move and again we need to kind of return the say that this is an error something as a chord right there just make sure you return that first and for for the delete is the same thing though just make sure the delete is the same thing as this guy but for delete there's no request right so it's just the id and the id is being passed along and i want to also just copy everything here same thing same thing but just make sure that just make sure that your your here we don't have a request right so remember this is going to try an error but remember we passed the id along on the route okay on the end on the route so it is actually from the route okay like that we're getting it from the route like that okay so it's right there and it's going to delete actually there's something there's some you can actually decorate these two like this too from like say from oh what am i doing you can do like that from route there's something here from route so it's going to take it from the route and pass it to the id like that actually i think this is more appropriate so the whole configuration and the whole system the whole application knows that it's coming from the route and then if this is not available in database just tell them the user is not found users not found that means we cannot delete it's gonna return not found but if the user is found again we're gonna do the dbcontext.entry and pass the user now this time around instead of entity state internet entity state.modified is going to be entity state.deleted okay for delete so it's the same thing like that so the same thing like that top one so it's same thing right so it's db context m3 dot entry you pass it to your user the entry you're trying to modify or the state you're trying to change dot state equals to entity state or deleted this camera is not modified it's deleted for delete and then we're going to save changes and this will delete it and then again i want to get that user okay i want to get this user all right so that does it for the whole theme and let's go test this now let's take it for a ride okay so we've already tested get user right so we're not going to test that so next thing we're going to create a user and that's our next target this is exciting right it's exciting we're almost done so let's just test it and if everything is working fine then we are done and then congratulations you've been able to create a working api and working api using entity framework core database first approach and using your regular your um asp.net core as well all right so this this application has loaded and then let's go to our postman again the gate we already tested the gate is always coming okay the gate the get came with all those three things in our database these three records right here in the database so that's fine uh so now we're going to create so we have to record three records right now id number three the last id number six is the last one so we're going to add in this one it's gonna be id number eight or seven or nine or something i think i deleted something so so let's do the the post so for this post let's change this so this this was the last one that i posted right so let's change the name to username to i love coding that's interesting right i love coding all right so um so let's give it a name as clock clock clock lower okay this person will be in alexandria virginia alexandra oh sorry that's not how to spell alexander alexandria virginia usa here all right so this is that and then i'm going to save it i have a breakpoint so it's going to hit our breakpoint so let's go and save this person we should have another record with maybe okay so we have this in our request body see it's right there everything is coming no id because this is creates okay and my database is actually set up to auto auto increment i don't have to pass in the id in database table so this everything is good here so it's going to pass it over to this table our class and then we're going to save it here and if nothing happens here then that means our connection is good everything was saved correctly i'm just going to continue so we should see that it has saved and it has given us a new record id number eight i love coding nice clock and all of that if we go to our database we should see that it's it's going to add default record and i'm just going to execute this query so it added the fault record i love coding clark law alexandria and all of that so our connection is working and our api is working so i'm going to remove this breakpoint just that so the next thing we're going to do is to update that last record we added this i love coding record we're going to change it to something else let's just change it to something else i don't know what we're going to change it to um so i'm going to copy this no not that one i like to copy everything because we need the id this time around because we're going to be updating the particular recorder we need the id so i'm going to update this guy here i'm going to be updating this all right how about we delete everything so we don't we don't get confused let's just can i delete it can i remove it it looks like i can remove it so let's leave it like that it's okay let's just leave it like that we know what you're trying to change so let's try to change this guy here okay so i'm changing this guy here to id number eight okay don't change the id the id is the target you're targeting record for id number eight and then i want to change username to um clock my clock can we just do that my clock my clock one two that's my username right and we're going to change the last name for clark it to be to be george so clark george now from from what he was before lloyd to george and we're going to say that the person lives in springfield springfield virginia or springfield virginia springfield virginia so so we change we change username we change the the last name and everything else is the same now let's take note of what it was before it was i love coding it was clark lloyd and springfield and alexander now this is going to change the springfield and all of that so let's go and update the record and there you see that it's going to update correctly now the breakpoint is here um i'd like to put a breakpoint so we could see i don't just want to update it like that then you don't know what's going on and then all of a sudden something changes uh okay so it's we have it as alexander right now but we're going to change it to to this guy here springfield and then my clock 2 and all of that okay so let's go on so there's a record that means everything is should be fine that saves correctly so that's saved correctly so everything should be fine and then when we go look at the record number eight it changed to my clock two springfield virginia and george is the last name now when we'll go and run the record you're going to see that it's going to change the spring field this should change to judge so let's run it again so this change to my clock this change to george and springfield so that is working correctly let's go delete the they did the last row the last record and that will conclude our our um edition our our recording for okay so id number eight we're trying to delete id number eight okay fingers crossed id number eight is going away okay so it hits the breakpoint right there and we have the id see from body from route it says from route right we have the body we have the id right there and then we're gonna get it in the database make sure that we actually that record exists first before we do anything like delete okay so the record exists you could see that the record exists see springfield usa number eight and george okay that record exists and you could you cannot go ahead and delete it now um so let's go ahead and delete remember entity state is deleted now once you save changes it has saved to database and that record should not be there anymore so we don't have that record anymore you could see that the record is gone and in database it is i'm gonna refresh it now you're going to see the database is going to so we're back to three records the database is gone well congratulations for learning how to program um an api how to create your own api build your own api and not just only that you learn how you you've studied and you know how to build your own api with database um entity framework database first approach okay so enter the framework called database first approach this is nice and this is really clean code that we just wrote here and i wish that i hope that this actually helped you to learn and how lynch helps you to understand how to create apis in the future if your company asks you to create an api this is the basics you know sometimes your company will have different logic and different things but this is just the basic of how to get from database and how to create a user like how to post a user create a user create a product or create create anything creatable usually over and then how to update that record and then how to delete the record as well right here so congratulations for achieving all of these i'm happy to have been the one to teach you and um next week or the next my next video will be coming out about a week from now next week um i'll be teaching you my next video i'll be teaching you how to consume this api so we're going to use the same api so please please subscribe to my channel so that when i upload the next video how to how to um maybe by the time you watch this robot is probably out there already so i will be teaching you how to up um consume this api my next video okay so please subscribe to my channel like this video turn on your bell notification button so that you will see when i upload the next video on how to consume this api so you could actually know how to con how to write an api and how to consume an api so we will be building a web application that consumes this this app this api uh just a re user will register and then we will save the user registration and then we'll take the user today take the user to their um to their profile user profile and then they can edit as well and delete as well so that's that's what it is so thank you very much for watching with me i really appreciate you guys and peace out have a lovely day wherever you are alright peace out
Info
Channel: Rock Nation
Views: 29,469
Rating: undefined out of 5
Keywords:
Id: 1WW6cEIbyqQ
Channel Id: undefined
Length: 75min 41sec (4541 seconds)
Published: Sat Jul 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.