ASP.NET Core Web API .NET 8 2024 - 25. Many-To-Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so this video we are going to be implementing a many to many relationship and many to many relationships are great for things like favorites likes invites or in our case a stock portfolio we want the user to be able to add infinite combinations of stocks to their portfolio and we want other users to be able to add stocks as well too so we need literally endless combinations of stock Bo and one to many has its limitations so this is a one to many and one too many is bad because in our case you can only have one stock as the parent so we wouldn't be able to have the infinite combinations like we want but there is different ways to solve this and this is a way I would not recommend this not do not do this what this entails is basically putting putting a user ID on the stock putting a stock ID on the user and tying them together this way but this is not ideal and people do do this but I would never recommend this not unless you have a special case the way that we want to do it is we want to create this thing called a join table and what a join table is going to do is it's going to have a user ID it's going to have a stock ID let me if I can get rid of this so it's going to have a stock ID and we can the user and the stock together in this join table and whenever we want to find out if there's a relationship or whenever we want to find out if our user has a stock in their portfolio we just refer to this join table and we refer to the relationship that way so we are going to be creating a many to many join table with some convention we are going to allow EF core to do a lot of the heavy lifting for us we're going to wire some of it up in on model creating but we're going to rely on some convention and all convention just means is that you put your IDs in the model you put your navigation properties in the model and EF core is going to take care of a lot of it for you and create this join table so that you don't have to and lastly once we get done with the convention we're going to come back in with the on model creating we're going to go into our application DB context and we are going to wire up our actual join table within the on model creating but that's enough for now let's go ahead let's hop inside vs and let's do some coding so the first thing that I'm going to do is I am going to go within my API I'm going to find the models folder and then I'm going to add the joint table and the join table that we are going to add is going to be called portfolio you could call this app user stock and if you do it this way Entity framework will take care of the whole entire thing for for you but I don't think that that's a very good idea so I'm going to call it portfolio because I think it's a little bit more telling of what it actually is here's where we're going to actually set up the foreign keys that are going to link the stock table and the user table so we have essentially what we're going to have is we're going to have a foreign key for our app user ID then we're going to go down here and we're going to have the exact same thing but it's going to be for the stock ID then here is where we're going to have our navigation properties and this is just for you as the developer you could technically just have the app user ID and the stock ID and use the on model creating to actually do everything but like I said in this case we're going to let convention do some of it the navigation properties are just for you as the developer so we're going to go into here and I'm going to say stock and stock and if you look at it it kind of does just look like a joint table you have the stock ID and you can kind of tell that these things are probably going to be tied together somehow but it doesn't stop there we also have to add the list and we also have to add the portfolios to the actual app user so we will have a public we will have a list and this is going to be a portfolio and we're going to call this let's see portfolios and we'll have get set and we will just for uh good time sake add a list here so that we don't get any null reference errors say portfolio and then we'll just go ahead and new up a nice little uh list of portfolios within our model then we're going to do the exact same thing for the stock and we can actually just go down here and we can copy and paste it since it's going to be the exact same thing so make sure that you have your list within your stock also make sure that you have your list within your app user and you should be good to go so the next thing that I'm going to do is is I am going to add the actual table names to all of the tables besides the user I think it makes it look a little bit better when you uh control what you name the tables and it also is great because for some reason and many to many relationships for some reason it always messes up with the table naming so uh this is not required but I'm going to go ahead and do this because I think it makes a little bit makes it look a little bit better I'm going to go into the stock I'm going to do the exact same thing I'm going to go ahead add a table and I'm going to call this stocks and this is purely just for aesthetic purposes so stocks portfolios comments looking good now the last thing that we need to do is we need to go inside of our application DB we need to add the new DB set because we're going to be accessing this through uh Entity framework so we need to add this to our DB set I'm going to call this portfolio and I'm going to call this portfolios and say Get Set and that looks good and lastly we're going to set up the on model creating so I'm going to go Builder say entity is equal to portfolio and then within here this is where we're going to set up the forign key so I'm going to go has key let's see has key B p is equal to new P.A user ID and then we're going to put p. the stock ID going to go over here and close this out with a semicolon so now that we have the foreign Keys declared now what we need to do is we need to actually connect them to the table so we're going to go entity and say portfolio then go down here I'm going to say dot has one say U is equal to u.a user then go down here we're going to say with many then go u. portfolios looking good and go down here and say has forign key so go down here foreign key then we'll say p is equal to p. user ID app user ID okay that looks good we've got that one done now all we need to do is contrl C we're going to go down contrl V go ahead copy that I'm going to go ahead and move this over move this over one to make it look a little bit better and now we're going to switch it out for the stock so we'll say stock portfolios is what we want and we're going to go stock ID that looks good everything else is looking fine so what I'm going to do next is I'm going to delete the migrations and I'm going to delete the database this is perfectly fine if you are working by yourself do not do this if you are working on a project with other software developers but just deleting the migrations and deleting the database is perfectly fine and it's actually nice because if you change the table names and you do the one to many or you do the many to many relationships you get a lot of invalid object there so I just went ahead and deleted the migrations folder then I'm going to go inside of the SQL Server management Studio and then I'm going to delete the database and we're just going to wipe the Slate clean so I'm going to go ahead connect going to go inside of the database and I'm going to go ahead in here and I'm just going to go ahead close all the existing connections and then I am going to go into here and I'm going to rerun all the commands I'm going to go net EF migration and I'm going to say add portfolio many to many just like this so net EF migrations add many to many looks like it ran perfectly fine then I'm going to say netf database update and hopefully fingers cross everything should run correctly I'm going to go down here I'm going to restart SQL Server management studio so go back and let's make sure that our database is the way that we want it so go inside here going to connect and we have fin shark we've got hopefully got all of our tables and lastly let's make sure that our naming is right so comments portfolios stocks then go into the actual portfolio right here and make sure that we have the app user and we have the stock ID and everything is looking good anyways hope that you guys enjoyed this make sure to smash that like button smash that subscribe button and as always thank you for watching
Info
Channel: Teddy Smith
Views: 2,234
Rating: undefined out of 5
Keywords: software development, programming, engineering
Id: Cxf_CKpZxQY
Channel Id: undefined
Length: 9min 56sec (596 seconds)
Published: Fri Jan 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.