Dot NET Core Web API using VS Code (Entity Framework DB First + CRUD Actions + Testing with Swagger)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys good morning welcome to new york case this is our asp.net core tutorial so part of this video i am going to create one web api using visual studio code and the database already i'm having the sql server database and the database connectivity i am going to use this oram entity framework so as i mentioned i am already having database so i am going to use this database first approach here i am listed out all the steps we can do one by one before that i want conveyor information in my last video also i am already created on web ap so that time i am used to visual studio 2022 so in the visual studio we have the templates for creating any files so in the vs code we mostly depending on the command prompt so let me start my first sister so if you want create any dotnet core application first you should install this dartmouth core sdk in your machine so i am already installed in my machine i am not going to install so in case if you want verify whether it's installed or not so we have the command is dotnet and version so see now it's showing the version is 6.0.301 so i am already have the dotnet code so the next step is we have to create our baby ap application using this below command so see the command dotnet new so after that we have to provide the tag name so as i mentioned i'm going to create the web apa so the tag name is babe ap so let me open this file studio code and i'm going to the terminal let me open the new terminal first i need to select the folder path i am going to use this f colon only so inside the f column i am already have the folder is lancor here i am going to create one more folder product apa so i am going to create file in the visual studio code so i am providing this bs so now let me copy this path here i am going to select the path now i can execute the command so already we have the command dot net new webb so you can see the progress so look like our application is created let me go to the folder see now the required files are generated now we can open the application here file and folder so our folder is product apa vs so here we have to you like yes i trust the authors okay fine so in the right side you can see the application product apa and see the folders like controllers and properties app settings.json and our program.cs and also the default model the weather forecast was available and also in the controller we have the default controller weather forecast controller so now i am going to run this application so after that we can include this entity framework for running this dartmouth core application we have the command is dotnet run see now our application is running and our url is 7090 https url so let me provide here http yes add the localhost so the url is 7090 okay fine so in the dartmouth core bb ap the swagger is defaultly implemented so let me enter the swagger url see now so we have only one default controller that is weather forecast that's the name is coming so it contains only one get method for testing this one we can click this try it out and execute so it will return some values so this is actually the method so it's returning some value that only we are just verified in our browser so the datas are coming from this method only so anyway this is kind of hardcode value but the real-time implementation we need to connect the actual database so as i mentioned i am having only sql server so initially i mentioned i am going to use this entity framework database first approach so i need to install this entity framework in our application so let me start the application and one more thing we have to enable to instance so it's basically supporting for our visual studio code development for the dartmouth application the first one is fisher you just to search it the first one so i am already installed in my machine that's what we have the option is uninstall under disabled if you are not insult so just click and install in your visual studio code and the second one is new get package manager so again this first one so you want to install it okay done we have installed basically it's supporting for installing this nokia packages so the step we have created the application and we run the application and also we have installed this extension so let me move on the next step so for installing this entity framework we have to execute this command so the command is dotnet tool install global.net entity framework so let me copy this command okay the dartmouth editor framework is already installed so for the testing purpose i am already verified so it's installed in my mission and the second one is we how to include this package microsoft entity framework core.design okay this is done so after that we need to verify whether the entity framework is installed or not so the command is dotnet entity framework so see now so it's saying the entity framework or dotnet command line 6.0.6 the latest version of entity framework installed in on my machine so once it's done we have to execute this scaffold command so the syntax is same almost to be used in this visual studio 2022 but some difference was there so let me copy this one and i will explain so the starting we have to provide this.net entity framework so in the visual studio 2022 we have to use like scaffold then hyphen db context here the command is db context scaffold and then we have to pass our connection string and we have to mention the provider name microsoft entity framework dot sql server at the end we have to provide the directory where actually we need to generate the output files and also our db context file so if you give like this output dir it will generate like this in some cases i won't generate the db context only in the folder other files i won't create in the outside means so i need to provide this command so context directory so anyway i need to create my all the output files also in the same folder so i am going to use this command so at the end i will share all the commands in the description so you can use in your project okay unable to find the provider assembly microsoft entity framework for a sql server so we need to install this package so actually there are two ways for installing these packages one is like we have installed this design the same way we can install otherwise we already have a nuget package extension so using this nuget package extension also we can install so let me show you just to press ctrl shift p so here we have to select this new get package manager add packages okay in the search box you have to type like a entity framework see all the packages are coming entity framework correlational abstraction unless there is kill server so let me select this sql server if there is any dependency it will install automatically so we can install the latest one okay it's done so see there are unresolved dependencies please execute the restore command to continue okay the restore is completed so otherwise we have to provide the option called dotnet restore basically this command also restore the required packages okay great so once it's done we have to execute our scaffold command once again so the build is started actually we don't have the folder in models so this command basically will create the folder and also it will generate the required files so it's saying the security warning yeah so now look like our db context is created so in the db context we have the objects and also the connection string is included so the same message we already seen in the visual studio also so we all know uh we need to move this connection string into our app settings.json so we can do the thing so the connection string so here we need to provide the value so let me cut this one and i'm coming to this app settings.json okay this is fine let me remove the thing here actually i'm using this intelligent c sharpened legend so it's showing the options so in case if you are not insult this extension of c sharp in your machine this feature basically not available so don't forget to install this extension sheet so if you see here also they will provide the complete information uh so this is the light bait development tool will sit supporting for the debugging support for dartmouth core and supporting for the json related things and it's available for windows mac also linux okay done now we have included in the app settings.json so the next step is we how to include in the middleware for the db context so let me come here and builder dot services dot add db context and our db context name is land db so it's not coming the problem is it's available in the different name space so let me copy this one we can add the reference and also i'm going to add one more namespace for using this microsoft entity framework okay fine now we can include our db context for this database context we have to provide the connection string as the input so object art use sql server so inside that we have to pass the connection string so builder dot configuration dot get connection string and our connection string name is already we have declared in this app settings so this is all about the configuration now i am going to create my first controller so in the visual studio we have the template so here we don't have the templates i think there is no possibility uh if i know definitely i will cover in my next video so my controller name is product controller dot cs okay it's created and now i'm coming to the sample controller file i'm copying the whole content and i'm including this place so instead of this weather forecast i'm going to provide the name as product okay this is done actually the declaration is not required and in the constructor also i need to change the name this logger also it's not required but we are going to use the database context so again the problem is we have to include the namespace that's why it's not coming okay now it's fine so it's providing some suggestions yes db text so let me inject here the constructor now i'm going to setting the value so this dot underscore db context equal to so we have done and also we can change this get method so let me provide the name is get out okay fine so i am going to return the action result only so basically this get method is for returning all the existing products so this dot db context and my table name is product i mean product here so i have to take all the values now returning so i just completed the changes so let me verify whether it's returning the exact data from our database for running the application i already mentioned the command is dark netrunner okay the localhost is 7090 yes see now we have our product controller here and it has only one function this get all method so we can verify so i'm just click to this execute yes it's returning the data so there are two records available so it's just returning so this is just an own gate method the similar way we can create the other methods also and the next one is get by code so here also we can change okay fine so instead of this two list i am going to use this first star default and we need to pass the parameter and id equal to code okay we have done and also we can use this update and delete methods also http delete and also we can include in the router also otherwise it will throw the error the product is not equal to null then this dot underscore db conducts and remove product so once it's removed the how to return is true if it's not so i can return false so this is all about the delete method and the final one is the first method so let me provide the name as create again we need to mention our routing here so http post so from body so our class name is product only okay we have it and now here i am going to verify first i am checking whether the value is available or not in case it's available i'm just considering this is the updation it's not there i'm going to consider as the new record for adding this dot db context products dot add and the one more option is update so i'm just trying to update only one field name or else i can update the price also so the identity column basically we are unable to update at the end i am returning true so basically we need to verify whether it's saved or not we can use this try and catch block for the time construction i am going to continue my testing so again i need to run the command dotnet run okay this is fine again it's running the 7090 so let me represent it so see now now in the product service we have four methods one is for get trial and another is get by code the third one is remove the final one is for create so we already verified this one this get almost there so let me use the code and we can test the next function slide is one and two here i'm going to provide one okay so it's returning single object so in case if i'm providing true so it's returning here i'm trying to remove the one so it's written the response is true now if i'm going to the top i'm trying to execute the guitar method okay actually it's not removed it's having some issue yeah once it's saved uh we have to do this save changes it's not included that's why it's not affected the database okay fine again let me run and we can complete our testing okay again i am trying to delete the id1 okay it's done so we can verify see now we have only one record the final one is our post method i'm not sure how actually i written this one so anyway we can test it so the apple amount is 100 so it's just a product so i'm just provided so anyway we have to execute this catal method then only know whether it's added or not yeah it's included the id is three and after 100 so let me have the same value here i'm to provide this id also so the price i am trying to give 1.5 so this is the update scenario again i'm got the response is true only so see now the price value is updated so now we have completed all the http viewer one is for get and another one is get back code remove and the post in the post we have covered this foot and post also so hopefully you got some better idea about how to create the application using this visual studio and how to use the commands so we have used n number of commands so anyway i will share everything in the description uh so continuously if you are doing some development in any of the application you will also remember the commands and the one more thing don't forget to install this extension c sharp so if you are not installed it cannot give any suggestions so you have to type everything manually so if you are typing manually you will face some camel case issue because it's not showing any error also while running time only so we can see the errors and also the steps i will provide all the steps in the description you can follow it so before closing this session i how to provide some information so i am already created the bb using visual studio also you may have the questions which one is better and so my suggestion is like so i am coming from this dartmouth background so initially itself i am using this visual studio so visual studio is really good so only problem is so if you are using this visual studio 2022 so it will occupy more space it will create some performance issue in this mission so this visual studio code is very lightweight and so whatever it is we can do in the visual studio the same thing we can achieve in the visual studio code also but we are using this command prompt some features actually it's not available but it's not that much important and still if you have any doubts or clarification please post in the comments please please don't forget to subscribe my channel and also we have recently enabled our membership so if you really like to join in the membership you can join thank you thanks for watching
Info
Channel: Nihira Techiees
Views: 35,116
Rating: undefined out of 5
Keywords: Dot NET Core Web API using VS Code, create ASP.NET Core web api using visual studio code, Dot Net core using VS Code, Add Entity framework using VS Code, how to install packages in Vs Code, how to use C# extension in VS Code, how to create controller in VS Code, how to run dot core application in VS Code, how to develop dot net core application in VS Code, how to verify dot net core install or not, develop Dot Net Core application using Visual studio code, Dot net core in VS Code
Id: rqBuhiR1x0M
Channel Id: undefined
Length: 28min 5sec (1685 seconds)
Published: Thu Jun 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.