Relationship Mapping in Entity Framework & .NET 7: One-to-One, One-to-Many, Many-to-Many

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to our channel in today's video we will look at how to implement different types of relationships in entity from a core using donate 7 API we will focus on three main types of relationships namely one to one one too many and minutes many but first things first let us create our Donut 7 web API project using visual studio 2022 so select create new project we are going to select the API project then we're going to cover this relationships demo to the API and I just leave it as default I'm going to use donate 7 and default it to the way Microsoft has that then we're going to create the application so we have our app project created now we're going to install the necessary packages that will enable us to use entity frame and code so I'm going to go in dependencies and I would say manage nougat packages then we're going to let's look for The Entity from a clock so I think we'll just install the provider which is the SQL Server and also I think we're going to bring in the tools except that another twos which will enable us to do our migrations that's done we are going to set up our models in our data context so create a folder let's call that folder models so we're going to call that models so we'll create models also book and publisher for us to demonstrate these relationship types so let's start by creating the author create a class we'll close this author and I'll keep it bare minimum because I just want us to focus on how do we do that shift so I'm gonna have an ID so I'm gonna have ID and also wanted to have the name of the author then we're going to create a book model and this one likewise is going to have an ID and we're gonna have MacBook title then I'm going to create publishes model so let's create that class so this publisher this will have an ID and name now that I'm going to set let's create a DB context so this context is what we use to access the actual database so it's just a class not just enhancement into the framework so I'll call this updb context could be any name as long as it makes sense to you and FTP context needs to inherit from the DB context then we do have a Constructor and we use the DB context object be called exceptions and this is texting the the name of the context okay my bad so let's just copy that there and bring it here options simple then the best itself also requires an option so we're gonna pass it there now for cleanliness I'll just do it like that so there will be some things we need to do when we want to create our model there are some things I want to do things like setup let's say what is the primary key of a field things like how do you want to do this relationships so these things are done via the by overriding the creating options Builder so what I want us to do first is to override that so there'll be an override and it's called model Builder creation so what will happen here is we're going to you know put our configurations put our configurations computer configurations could be um implementations whatever you call it but before we start putting along with those configurations we need to tell it what tables we want to have so we have what we call a DB set of what we want to have in our database so we're going to have author call this authors uh you want to have the one for the DB set cookbook I'll call this books and we also want to have one for Publishers so publisher and like so So within this model creating we are trying to say as you're creating because now in the framework uses conversion so it's a way it does things but I was saying as you're creating these guys we want you to also put there the following uh the following configuration so the first configuration would be [Music] in our models I want this field to be to be the primary key and I want this field to be required so that it's not optional so what we'll do here is we'll say we want to write on this model builder so on the model builder uh give me an entity so we can start with any entity outside of the author okay so now we are doing one penetration so say author within the author I want to create an entity and like so okay so we'll do it for author and we'll do this for all three of them so I went for author one for publisher okay and one for publishing all right so for the author I want the ID to become the primary key so say entity itself I want you to get me the product code has key so as key just says this is your primary key so what do you want to be your primary key so in this case I won't see ID to be the primary key then I'll go also to another property of his name and I want to make it required so that every single time you put a you inside a row in that database table it should always be there otherwise it will throw an error because you can have a another name so I say get me the entity as well and give me the property and this property should be uh name and I want this to be dot is required so this will make it a we'll make it required I'll go into the SIM for the book I said book dot has key and I would say a in the book also one t i did software key as well as make the title uh required I'll say entity that would give me the property and I want to play with the title and in the title I want that to be required and since um author in the entity and and publisher the same might as well club and pass so here I've said when you go and create these models make sure these properties are there so what's next we want to bring in our first type of relationship which is one to one we will use the author and publisher to show this one-to-one whereby one author who have one publisher first of all we need to have it clear that in our models it has to also know that okay Finance author exists they publish exist so it means if an author has a publisher we need to link them here all right so I'll say and also we also have a publisher and I'll call this publisher okay site and for us to navigate that we need to have a way we'll link that for us to have a foreign key to that table which will be covered publisher ID or do the same in the publish it also know that it has an author so I say prop osal what's up also bring in the author ID bring in the author ID then we need to set up this relationship in our one model create so that it knows so since we've already started here with this entity we might as well continue there then you would say on this entity right I would say it has one and obviously it has one uh publisher our relationship it has one publisher then I say with one word with one author if you guess that you're correct so you see that I'm bringing a certain a different order so this is a for the publisher this B is for the author so distinction then it needs to have a foreign key and on What entity we have the foreign key it will be on the publisher entity so on the publisher right and what is the foreign key there to link it with this one so we'll go and say on the publisher use the publisher rather than author ID sorry and something very important is when we delete this we don't want it to Cascade so on delete we'll just say the delete Behavior will be do nothing so there you have it we have links that as a one-to-one very simple and very straightforward now we want to implement one to many so how can we do one to many and we're going to use author in books to show this type of relationship so where one author could write many books again we have to start from the models and really key in and bring this type of relationship so I'll start with the author so since an author will have many books and obviously that won't just be an object it will be a list right so say a prop then I collection I collection of book right and I'll call this books so I'm saying an author will have books then also I have to go to the book itself and also point out that you know what you're gonna have an offer so we need to have an author here and also another ID that will be the foreign key so prop ER and author ID now this has been done now we need to also key it in into our own model creating so that will be on the book that'll be on the book as well as uh so we can do that on a book sale and one book has many authors that's possible to do with that so I also come here and say get me that entity it has one so one what one author right as one then I'll bring in uh I could say a DOT awesome but instead of saying with one here if you guessed it we are going to say with mini then with many we're going to say give us a p with many words with many books which is coming from the the books that that's an author and we're going to have as foreign key and the final key here will be a dot or the ID again I'll put the on delete and say they did Behavior here is going to be no action and voila you've implemented your one to many now let's go to Major meaning uh but first Implement menu we use books and Publishers so many books can be done by many Publishers I've been publishing publish many books but to do this since it's a major menu we need to have a joint table so let's join table usually just texts the names of the two tables the book and the publisher we just call it book publisher and the keys or the primary keys of that table will just be the two primary keys of both tables in this table there'll be four in keys so what we'll do here is well first of all create that model we'll call it book publisher socket model call that book publisher like so and within the book publisher we just want to have the IDS of the subsequent double Source prop but since we want to navigate that then I'll have book then I'll say book then a half book then I'll do the same for prop publisher publisher publisher but our primary keys are going to be are going to be the book IDs Source prop int book ID as well as prop and publish ID so this guy and that guy will be your primary keys so now since it's a joint tab it means from the book the book publisher it will be a one to many and from the publisher to the book publisher it'll also appear one too many so we need to go ahead and do that but before we do that the books the book and the publish also need to know about this step so it will be a one-to-man so we have to go to the book itself and we're going to bring in an Eye collection so prop an Eye collection of Google publisher and I'll call this book publishers then I'll copy this and have the same now publish then once you've done that we need to link this relationship in our entity class rather in our model creating so what we'll do is we'll need to bring another leadership Source say model but before I even do that if we are the for our logic we have to also introduce it as a DP set there so I'll say prop uh the B set then I'll call this book publisher and this will be called Publishers great that's done now let's write on that model builder so model builder model builder entity then this entity will be publisher I'll call this entity then we're going to just do the same we did there then I'll say first of all we have to bring in the key property so the entity itself has key and unlike the other tables on top there where they're just one key of one one column in this case we're going to use two so what I'll do is say BP and then I'm going to say new and I'll do this then I'll say BP Dot publish ID and PP dots book ID switch so here we're using this almost object just to say this tool be your your keys then we have to Now link that one to many for these two so I'll start with entity that has one so it has one then I'll say BP has one what two times one um BP Dot book then uh mini because it's going to be one too many many and I could say b equals to be Dot book publishes and let's see what has foreign key and this would be BP going to BP Dot book ID and then I also add the on delete say that I could use the no action and we'll close that so the other one is just pretty much similar we'll just change the book supervisor and make this publish ID so at this juncture we've done that but we need to create the actual database and it's not in our in our table yet but we've done our one-to-one we've done one too many and we've done our main too many so in our app settings I'm going to bring in the connection string which is going to enable us to do that so I'm going to call this default connection then uh what you do here you have your server so we use the SQL Server Express which comes with figure Studio we call this local DB or code DB then we're going to have Microsoft SQL Microsoft SQL what would it be we'll close that then I have database I'm going to call this EF relationships yeah relationships BB then I'll say trust connection rusted connection call that true then once we've done that since.net core writes on the dependency injection container then we have to add Entity framework on that container so that we cannot use it anywhere we want to use it in our application so I'm going to say get me the Builder and then services then idb context there for the DB context and record that updb context uh DB context then when I say options and I'm going to say options dots want to use SQL Server and we want to access that connection strings or say Builder dot configuration dot get connection string dot get connection string and uh Okay click connection string dot get custom string once we do that we want to bring that connection string inside of here because then we can uh proceed then we can proceed to bring that we can proceed to bring that and as I've done this then we have to run a migration so go to tools you'll get package manager package magic console and we'll say add a migration and we'll say created initial tables so then once that happens we'll see that it will show us what it wants to do so you see here it's creating authors table we've got the publisher which is linked to their publish ID we've got the books Publishers book publishers and the next sort of business is to actually now create this database and we do this by just saying update database then it will go ahead and it will create those tables for you and for us just to verify that we've done that we can say View SQL Server object Explorer SQL server and then we go to DB databases then we've got that EF photoshop's demo and this should have all those four tables plus your migration table which just tracks the history and they have it you've got those tables and so yes so there you have it we've successfully implemented one to one one-to-many and many many relationships using donate API I hope you find this tutorial helpful and if you go and if you did then consider subscribing and please if you wanna if you want us to make free content please definitely help us on our patreon page would be very grateful and happy coding
Info
Channel: Code Unparalleled
Views: 3,667
Rating: undefined out of 5
Keywords: api management, code coverage, continuous delivery, api security, api gateway, c# online, asp.net mvc core, software project management, continuous integration, clean architecture, api testing, unit testing, clean code
Id: pqiUJP0gmOc
Channel Id: undefined
Length: 24min 9sec (1449 seconds)
Published: Wed May 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.