ASP.NET Core Web API .NET 8 2024 - 3. Entity Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I give this analogy of Excel spreadsheets being very similar to database tables database tables themselves are wonderful they're awesome but they have a downside and that downside is that you can't really do anything with them when you're coding a database table is not in a format that a software developer can actually work with hence why we have this thing called an object relational mapper now in net the object relational mapper is called Entity Framework and what Entity framework is going to do is take our database tables and turn them into objects now notice an object an object is actually just a sideways representation of one row in a database it's the perfect representation because it's just a sideways database row we get this perfect representation of just one row and we can even turn them into list so we can show more than one row in a database but we're getting ahead of ourselves let's not get carried away right now let's just worry about installing Entity framework because there kind of is a bunch of stuff that you need to install so these are the actual tools that we are about to install and we're just going to go ahead hop right into VSS code and start installing all of this okay so the first thing that we want to do is we want to add Nate Gallery if you haven't added Nate Gallery just go ahead to your extensions right here type in nougat Gallery it'll be the first one to come up and after you get Nate Gallery installed you want to press command shift p and command shift p is going to open nit gallery for you and let's go ahead and start installing SQL Server now it used to be where you had to install everything one by one but nowadays if you just install n framework core. SQL Server it does everything for you and we want to install eight make sure that if you're using seven or if you're using an older version match it up and I'll show you how to do it that right now so go to your api. CS prage we are using net 8 if you are using Net 7 you're going to have to change it otherwise it's going to give you errrors so make sure that you have the correct version and everything will work correctly just go ahead hit api. CSR and go ahead install next thing that we want to do is we want to type in tools and usually it comes up sometimes it doesn't the DU get gallery has pretty good search feature so usually you're good to go we're going to go ahead and install the tools and then we want to also install let me see here design and design once again very easy you should be good to go after that okay so the next thing that we want to do is we want to create a folder and we're going to call it data and we are going to add a new C folder called application DB context and and an application DB context is just a giant class that is going to allow you to search your individual tables as you can recall I talked about how an object relational mapper is going to transform database tables into objects you want to be able to pick out what you want and what you don't want and an application DB context is a giant object that's going to allow us to specify which table that we want and if it doesn't make sense just kind of follow along and things will start clicking here in a second and I'll explain things kind of piece by piece so first we need to inherit from the DB context and the DB context is going to bring in all types of goodies for us that are going to allow this to happen next thing we want to press C which is going to create a Constructor and if you didn't see what I did there you go c r and then you hit Tab and that's going to create a Constructor for you then what we're going to do is we're going to bring in DB context options we'll call this DB context options and then we are going to bring in what's called a base now a base is pretty much just you typing out the DB context like this but we can't do that in a Constructor so we have to make the base and the base is going to allow us to pass up our DB context into the DB context so watch this we have our own DB context and then it's going to to pass it up into the actual inherited DB context and that's all that base is doing you're just passing this piece of data into this right here so we're going to go DB context options we're going to pass in our options now what we need to do is we need to add these tables that I keep talking about I mentioned how the application DB context is going to allow us to access tables and we need to be able to actually do that so we wrap it in this thing called a DB set now DB set is kind of kind of just a fancy word for I'm going to grab something out of the database and you need to do something with it it's going to return the data in whatever form that you want but there's this thing called deferred execution we'll talk about deferred execution later on down the line but just realize that whenever you use DB set all that you're doing is you're just manipulating the whole entire table you are manipulating the whole entire stock table and this is actually going to create the database for us you may be asking yourself well Teddy we keep talking about this database but where is the database we're going to make it and this application DB context is going to allow us to do it through Entity framework basically going in here finding your tables for you and then it creates the database after it gets done searching for the tables so we're going to go into here we're going to have a get and a set and this is all we need right now these are the only two tables we're going to add extra tables down the line but we need a place to keep our stocks we need a place to keep our comments and this is the actual place where it's going to happen it's pretty much you linking up the database to the actual code sort of like you go into the back of the TV and we're actually going to have to do that right now we're going to have to go quote unquote into the back of the TV to hook up this database context that we just built so we're going to go into here make sure that you're placing this before the app builder. build otherwise it's not going to work you have to place it right here so we're going to go Builder we're going to have Services we're going to have ADD DB context we have to add our DB context and then this is where we pass in the application this is where we pass in what we just built so we're going to pass in application DB context we're going to go options error function bring this down then right here we're going to go ahead add a semicolon so it stops aing then here we're going to type in our options so we're going to go into here we're going to add our options and this is where you plug in which database that you want to use so we're going to use SQL Server we're going to go do into our Builder we're going to go configuration then we're going to get connection get our connection string and it automatically shows up for us and we're going to have default connection now this is probably confusing because we haven't actually built this yet this doesn't this thing right here is going to search within our app settings Json and we don't have actually have that built yet so we need to actually go in and add our connections but before we do that go ahead go down into your search bar right here and search for SQL Server management Studio go ahead fire up SQL Server management studio right now because we're going to have to do something very quickly so what you want to do is you want to go into your database you want to rightclick you want to add a new database and we're going to call this fin shark you can call it whatever you want to maybe you're making a running app or what whatever type of app that you are making but you can name this anything but you're going to have to match it to your connection string so the first thing that you want to do is you want to add connection strings make sure that this is plural right here if you don't spell this correctly it's not going to work and then we want to add what we added within our program.cs So within our program CS we have this default connection now you can add as many as you want to but in our case we just want to use default connection right here we want to use what we actually put in the program.cs then we're going to go into here and then we are going to add a connection string now finding a connection string is incredibly difficult so what I'm going to do is just paste in almost like a template and you can use this at your discretion so go ahead go ahead save that and this is where you want to change your desktop is going to be different so what you want to do is you want to go into fin shark rightclick click properties if you didn't see what I did there so just go in here right click click properties and go ahead and get your desktop and what you want to do is go back into here and paste it so we're going to go into here we're going to paste it as you can see it's the same exact one you want all of these you want make sure that you get trusted server uh trust server certificate you want to en don't encrypt uh intergrated security you want that to be true otherwise you're going to get a lot of errors and also this is the name of the database so if you named your database something different go ahead and change that and I'm going to leave this template string down in the description but remember remember if you use a different database you're kind of on your own every single database has a different connection string and it's almost impossible for me to predict connection strings unless you use SQL server in the way that I used it in the video okay and lastly this is where everything comes together if we did everything correctly we should get no errors we're going to do what's called a migration now I'm not a big proponent in learning how migrations work because I'm just going to be straight up you're probably not going to be using migrations in a work environment so the whole entire idea behind migrations is that migrations create the database for you behind the scenes and when you actually run the migration what's going to happen is that Microsoft is going to generate code to build the database but key point it's not going to actually build the database until you do the actual update so this is going to generate the code migrations generates the code to build the database but the database is not going to be built till you actually run netf database update so what we're going to do is we're going to go into here and we are going to type in net EF migrations we're going to go add Enit and if everything works correctly we should not see any errors please please please work correctly please yes okay thank you God okay when you run that it's going to generate this up here if you want to you can go ahead take a look inside of it and it kind of just makes sense you're building an entity there's a building entity for you but if you check the database so let's go back to our database rightclick click refresh go down you're not going to see any tables there's not going to be any tables in this database tables are where you're going to see your actual Excel spreadsheets but you're not going to see anything in there because we haven't run the actual database ads okay so the next thing that we need to do before we run the actual migration is we need to turn this to false we don't need to worry about globalization in this project we need to do is we need to go netf database update and if we've done everything correctly fingers crossed yes our database did end up being created so what we need to do is we need need to make sure that it actually was created so go back inside of SQL Server management Studio go ahead refresh we go to our tables and would you look at that we have our migrations history which just keeps track of all the migrations that we've done we have our comments and we need to make sure that we have our rows we have our relation picked up uh relationships picked up so if you didn't see what I did there right click go to select top 100 rows you could also edit it if you wanted to if you want to go ahead and add information but just right- clicking and using that select top 10,00 rows is very easy and very helpful and it's something that I use on a day-to-day basis so go ahead go in there make sure that everything is created and everything is looking good all our M our migration our migrations ran our database updated we are now ready to move on to the next stage of our app anyways hope that you guys enjoyed this if you did make sure to smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 4,391
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: SIQhe-yt3mA
Channel Id: undefined
Length: 12min 57sec (777 seconds)
Published: Wed Jan 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.