Scaffolding .NET Web APIs Powered By EF Core, SQL, & Azure in Seconds

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign oh hello there I didn't see you there welcome back to another delightful episode of Story Time building web apis with James montemagno today I'm going to show you how Visual Studio or the command line can easily enable you to scaffold out web apis in seconds so whether it's your first API or you've been building apis for years there's tons of cool tooling into Visual Studio to scaffold out your API and even connect you to databases with any framework core in seconds so tune in [Music] foreign [Music] I'm James and I'm back today with another video as a follow-up to one of my previous videos my seven favorite features in net 7. specifically sort of the new improvements to apis in.net and specifically minimal apis sort of that you know really low ceremony low code way of building apis which is like all line of code now I'll put that video up over here or down over here wherever that YouTube video shows up because you know YouTube now what I love about apis is how easy it is to actually get started not only is the API really easy to map gets inputs and deletes and all those things but there's great Tooling in visual studio with one click can actually scaffold your API and additionally it can connect it to Entity framework core database system for SQL Lite or local storage or even connected up to the cloud so that's what we're going to do today so let's get started all right I'm over here in Visual Studio 2022 I'm just going to go ahead and create a new project and we are going to type in web API there we go web API I'm going to use the c-sharp one today so let's do that I'm going to call it a monkey API we specifically created a monkey API earlier in the.net7 video so I'm going to create a new one here and then what we can do is create this and I have a bunch of options so I can zoom in here and you can see a bunch of different versions of net installed I'm going to go with.net7 you can enable different authentication types you can configure https you can enable Docker if you want over here you also have the option to enable controllers if you want that controller based Style with like MVC and controller base there or you can also uncheck it which is the default what I prefer which is the minimal API sort of low ceremony way of creating apis and a few lines of code I'm also going to enable open API support or Swagger support and if you don't want top level statements you did too lots of customization all the customization now you can actually create that entire thing from the command line as well totally up to you now let me go ahead and just double tap on this program CS over here and what we're going to see is that there's just sort of a few lines of code actually in here most of it is the class and then the get and there's really not a lot of ceremony here we have the map endpoint API Explorer Swagger gen we're gonna use Swagger and the Swagger UI that comes in that'll give us a nice sort of Explorer for our endpoints we got that https redirection and then here we have one map get which is the weather forecast if I come in I can say app Dot and I can say map and then in here there's all sorts of things like deletes there's gets there's puts there's posts there's all the verbs that you could imagine for restful services then it goes ahead and runs it and here we can see that whenever weather forecast is called this will go off randomly generate some forecasts and go to down there so let's go ahead and just run this here and see what happens because we have our Swagger gen up and running we should hopefully get a API that has one endpoint of weather forecast so Ryan's going to go ahead and debug up for me here there we go and there's our Swagger UI we can see our endpoint we're on Port 7066 monkey API and weather forecast there you go so here I can go ahead and hit try it out hit execute and just like that we have our weather coming in cool nothing fancy all right let's go ahead and add said a monkey so I'm going to go ahead and say add I'm going to add a new folder I'm going to say model and I'm going to go ahead and add a new class and I'm going to go ahead and call it a monkey there we go now I could go ahead and type all the monkey stuff but let's go ahead and just copy and paste in some code because that'll be a tiny bit easier I have a some demo files here so here's our monkey there we go let's go ahead and copy that monkey class in so nothing really fancy with my monkey I have an identifier as an ID name details images location population latitude and longitude so all the monkey data that you could possibly want all right now I'm going to pile that up make sure that that's all legit and it is cool so now we want to create an API well we would probably need some sort of database or something like that and we know we could do an app dot you know map get and then I could say you know slash API slash monkey or something like that and then return said monkey inside of here and I could basically create this API manually but Visual Studio will do it for us automatically I'm going to right click here and then I'm going to say add let's say new scaffolded item and the scaffolded item is something that for all intents and purposes Visual Studio inspects and says we can scaffold code for you or create code for you and there's a lot of options in here let me zoom in here there's things such as creating an API controller one with read write actions uh controllers with read write actions with Entity framework we have this API with read write endpoint so this is the minimal API and also one with any framework as well and that's one that we're going to pick so this is our create read update and delete endpoints and we just hit add now it's going to require a few other things from us so the first thing it's going to ask us is here what model do you want to scaffold so we could select that weather forecast let's select That Monkey it's going to want an endpoint class and we don't have an endpoint class so we're just going to create a new one monkey endpoints and that's going to basically create a file called monkey endpoints and then put all the stuff in there and that's going to need a data context and that data context is for Entity Framework so there we go we're also going to use open API and also this typed information as well that's really nice with irresult instance so we're going to hit add this is going to go ahead and go ahead and create all the nuget packages and pull those in for us and then scaffold the code and it's going to inspect everything with a monkey so we're totally good so here it's just going to update the dependency information all right so here we go our monkey code is here we can see that a monkey endpoints file has been created for us automatically let's go ahead and investigate that here we have this sort of uh monkey API uh here we can see we're using a bunch of Entity framework stuff down here we have the map monkey endpoints and this is sort of the new part of Net 7 with these apis you can map a group so what this means is that every sort of default route is going to be API monkey so don't need to repeat that over and over again so if you just wanted to be slash monkey it would just be slash monkey but here we'll do slash API slash monkey you can version these you can do a whole bunch of other things now what's cool here is it's created this API context and if we go to definition this is that DB context from coming from any framework core which will enable us to connect to all sorts of different databases such as SQL local storage and memory storage uh postgres Azure SQL all sorts of stuff and that's what that sort of monkey context does it enables us to do all sorts of database manipulation so we can see here is that this database is getting a list of monkeys and just says to list async here if we do a map get which is going to return some typed results here uh monkey back it will go ahead and find that monkey and if it is a monkey it'll return okay and a model and return it back it creates the names and the opening API and everything you need so that's sort of the same things we have our put down here where it tries to see if the monkey already exists and if it doesn't then it go ahead and gets it and updates it posts We'll add a new monkey into the database over here which is nice and even a delete so there we go so the only thing that this has done is if we go back into our program CS is that it has done a few things here it's gone ahead and added our database context and here we have use SQL server but here I can say use uh there's I guess memory cache for example and there's um like SQL Lite as well there's extensions you can have in there and pull in the different packages but we'll use SQL Server as the default the other thing that's been added here if we go down here is this one line of code which is calling that map monkey endpoint so that gives us everything that we need all right cool so now let's go ahead and run it and see what we get with literally just a few lines of code dropped in there we're going to now get our Swagger and we have all the monkey apis here's our get post get for a specific ID put and delete everything we need so if I go ahead and run this and execute it we're going to go ahead and see that we get an exception and we get an exception because we haven't specifically updated our schema of our database you know we just said hey here's our database it hasn't actually run any migrations on it so let's go ahead and do that now there's many ways of doing this including command line interfaces but I'm going to let the magic of Visual Studio walk me through the process over here on connected Services I'm going to go ahead and double tap on that this is one of my favorite hidden gems in Visual Studio this is what's going to show me all of my dependencies and services that I've registered I can actually click on this and add a bunch so if I want application insights if I want Azure storage signalr service a key vault in here here's all those different databases so there's the SQL Lite postgres in there Azure SQL there's right as cache mongodb rabbitmq Cosmos DB connectors Azure function connectors app service connectors all that stuff you can put them in there but when you look at these dependencies if I go ahead and zoom in here on this triple Dot there's things that you can do so you can edit the dependency you can disconnect you can actually add a migration update a database and here's what's really cool is you can actually generate a SQL script or even open in SQL Server object Explorer and look at the data let's go ahead and add a migration here and this is going to create our first migration and visual studio is going to look at my project it's going to look at everything that's going on and it's going to try to find that DB context for me which takes a second or so here's it sort of scans my code and tries to pull it in let's see if it'll find it there it is and all I got to do is hit finish I'm going to zoom in here because while it runs this uh migration we can see that it's actually just calling a command line.net EF which is a command line tool which adds this specific migration for the API context and then we're done now once we've created it we're going to update the database with this new migration so again this is going to go off it's going to find my DB context class names and there it is it's going to look for all of my migrations that I have so it sort of looks and there could be a bunch of them in there and it's going to run yet another EF command which is net EF database update context right there and it runs it again boom all right so now I can go ahead and run that API yet again and now when I go ahead and get said Monkeys just like that we get nothing back because there's no monkeys in the database which makes a lot of sense so we can go ahead and add those monkeys into the database so we could go in and you know post a monkey here for example and put in whatever this monkey is but what I'm going to do is I'm actually going to run a script down over here so let's go ahead and open up vs code here we go and I have this little fill monkey over here and all this needs to do is simply go off and call localhost and a specific Port so let's go ahead and grab that one 7066 perfect and what this is going to do is it's going to find monkeys in this monkey Json file and it's going to go ahead and basically call that API endpoint here and it's very lucky that I call that API monkey so let's go ahead and save that there I'm going to go ahead and say new terminal and I'm just going to say dot slash fill monkeys there oh let's go ahead and change this over to a new power shell there we go dot slash fillmonkeys there we go and now that's going to go ahead and call those apis if I go back over into my Swagger UI I can hit that execute again and just like that have all those monkeys coming into the application which is super duper cool now again I can come into here and I could actually go ahead and generate a SQL script for my migrations if I want to put that uh you know somewhere else I can also open this inside of Visual Studio as well so here we can look at our databases that are in here uh where is it at there it is monkey API just like that we can look at our tables and here's our monkey table and I could go ahead and view data and there we go there's our data right inside of visual studio so it's really nice that integrated development experience we get all of those things right there for us now one other thing that would be really neat is actually connecting this up to the cloud so what I've done over inside of the Azure portals I've created a monkey's DB database and a SQL Server so that's gonna have a SQL database and it's there you can open the query editor and let me just go ahead and type in my password Here and what this is going to do is show us let's go ahead and allow that to our list and okay it and we can see our tables there is so far nothing in it it's completely empty and it's empty because I haven't had anything into it yet at all so let's go ahead and actually connect the actual database here in our API to Azure and then we could deploy it as well so what I'm going to do over here is I'm going to go back into my connected services and we're going to add and this instance we're going to add where's it at Azure SQL database so now since I'm already logged in It'll ask for my subscription so I'm going to go to my visual studio Enterprise and I've created that database now the cool part is that Visual Studio could create that database as well you could just hit create new and it'll create the entire database for you here I'm just going to go ahead and tap on this and connect to it now it's going to need a connection strings I'm going to call it Azure database that sounds legit and we have a few options here so there's a username so I'm going to go ahead and type that in and I'm going to type in my password as well that I set up earlier I also have some options like local user storage or Azure key Vault let's go ahead and just do local and hit next and it will go ahead and add now the nuget packages secrets and the secrets Json local file so I'm just going to hit finish here there we go it's also going to say hey we need a firewall rule so let's go ahead and set that up so kind of nice visual studio kind of does that for you automatically and then it will hit close that's it now if I go back over to my program uh CS I can go ahead and update specifically this connection string from Monkey API context to Azure database here and all we have to do now is simply run it now we've connected up it's going to use a different connection string and just like that we'll go ahead and connect to that database now if you've been watching literally any of this video so far you may have some idea of exactly what is going to happen because when I hit this API and I hit try it out and hit execute well we haven't done anything with the database I just showed you that I don't have anything in fact I get an exception there's no monkey found in the database so let's go ahead and create it yet one more time so let's do it all right what we're going to do now is come back into our connected services and you guessed it just like I went ahead and added the migration I can add the migration and update the database here I can still open the SQL Server uh object Explorer generate that SQL script or even open in the Azure portal so let's go ahead and add the migration this is going to do the same thing that we had earlier but notice it's named it something different if I zoom in here it's called the Azure migration because it has a different context as far as what database it is updating we may have updated our local one and not updated our Azure one so it needs to sort of track those two different migrations we've created it now we need to do is update the database let's go ahead and let it find it here and again this will not only find that and we'll find our migrations that we just created so if we didn't create more you know wouldn't have any but if we added another one after this added or removed a property would migrate as well hit finish it's running that script but this time it's running it so it's automatically running it on that database for us so it knows how to scaffold it out into our database and we will be good to go all right let's go ahead and load this up this time I'm going to go into our monkey endpoints and I'm going to go ahead and just add a break point here on our code there we go right there perfect okay so now I'm going to hit get I'm going to hit try it out execute and we should get basically zero monkeys back so if I go here and hit View here we get no monkeys in our innumerable visualizer let's go ahead and this time see that we have hasn't connected to the database because I probably paused it so let's go ahead and do it one more time there we go no monkeys Perfect all right cool so now we're going to do is come over into our vs code instance run that script again I guess I could have run it from Visual Studio there's a terminal there but now when I run this again we can actually go ahead and see this monkey code I can actually export this to CSV or Excel that's super cool this innumerable visualizer and go ahead and return it and just like that well if I would wouldn't have the break point I guess where it's breaking it in the code let's go ahead and do it one more time and continue on there we go we're going to see that we get all of our monkeys back now if I go back over into the Azure uh instance over here and refresh this we can actually look at our tables and prove it to you that here's my monkey I can go in and select the top 1000 rows and there we go there's all the monkeys that are right there in our database up in azure all right there you have it it's sort of the power of a beautiful amazing IDE in visual studio now there are tons of command line stuff that you can run too but as I sort of showed you all those cool Integrations all those things as well all in Visual Studio 2022. I will leave some uh links to the source code but also to the documentation for all this cool stuff and also to some of the sessions from dot net comp 2022 showing off some of the really cool new API features I really hope that you enjoyed this video I was inspired because I did some Keynotes showing off these features and Niche from my team really wanted me to create a video so I was like I guess I'll do it sure um I love this and I just love sort of the seamless integration here there's some other steps that I want to get into such as automatically configuring CI and CD for this puppy and putting it up into Azure automatically with GitHub actions but that's for another video if you want to see that or other things let me know in the comments below if you're using this stuff let me know as well love to hear back if there's other cool Integrations in Visual Studio that you're using alright don't forget as always to you know gently a tap that like button down there you can smash it if you want or you can just tap on it's up to you it's your day and also if you want to subscribe because I put up videos all throughout every single week go ahead and you know click on that subscribe button too you get notified become part of the notification Squad so you get notified whenever a new video comes out right on this channel anyways super appreciate you watching and have a good one [Music] thank you [Music] foreign [Music]
Info
Channel: James Montemagno
Views: 79,239
Rating: undefined out of 5
Keywords: c#, .net, .net core, ef core, asp.net web api, asp.net rest api, scaffold api, visual studio scaffold, generate api, api generator, web api generator, asp.net core web api tutorial, azure sql database, c# programming, c# tutorial, asp.net, asp.net core web api, c sharp, .netcore, visual studio, programming, asp.net core web api tutorial for beginners, restful web api tutorial asp.net core, api, development, asp.net minimal api, nick chapsas
Id: 32iBQvIZQxU
Channel Id: undefined
Length: 20min 54sec (1254 seconds)
Published: Fri Dec 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.