Scaffold Existing Database - Entity Framework Core | Trevoir Williams

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so in this lesson we're going to be looking at how we can reverse engineer a database that exists so once again we have code first where we can write the code that generates the database but in some situations you might already have a database that you wish to scaffold out into your model your class models like what we have been doing up until this point so in this lesson we'll be looking at that and you would have taken note that i have already created a new project so you can do this if you want to follow along but it's not absolutely required because we're just going to look at the command and see exactly what it does so i've created this new console app project which is solely for the purpose of scaffolding the db so you can see exactly what happens so i'm going to make it my startup project and we're going to go over to the package manager and just as a refresher i'm going to say get dash help and we're going to take a look at the command to scaffold the database so here's the list of the commands and we see here that we can say scaffold db context so that's the one that we're going to be paying attention to in this lesson so when we want to scaffold i'm just going to copy it and paste it as the next command i'm going to run and we're going to use the same database that we're working on in this entire course which is our football league database i'm going to scaffold it into the models all right so i need to specify the provider so hyphen provider so this is a parameter provider and our provider will be microsoft dot entity framework core dot sql server all right so that's the name of our package and that is our provider so that just goes to show that this scaffold db context command could be used for other databases that are not necessarily sql server all right so we specify the provider and then we're going to have to give it the connection now the connection here would be the same connection that we're pretty much using in our db context so you just have to formulate a connection string since i already have one i'm not going to type it from scratch i'm just going to copy it over because like i said we're using the same database but whatever database it is you write a connection string data source is equal to the server it's on initial catalog is equal to the name of the database because it might be on a server somewhere else it might not necessarily be on the same server so you just want to take note of the fact that you write that connection string according to where that database is so i'm going to open quotation marks put in my connection string close quotation marks so one more thing that we want to do before we try to execute this command we want to make sure that our project references the same libraries that we had to set up when we were doing our initial scaffolding or our initial migrations right so if we take a look back in the data we see that we had to reference the sql server and we had to reference the tools and tools references design all of these are needed i'm just going to take this entire item group copy and then go over to our new our new project and paste and then do a quick build and then after that has been successful we're going to come back to the package manager console i'm just going to double check that the provider is correct and the connection is correct i want to make sure that the default project or the target project is the project that we want to do the scaffolding to and then i'm going to press enter all right and after pressing enter it did a build it gave me a nice warning and then a very deadly exception now like i said i'm not going to shy away from errors in this course i think seeing the errors and working your way through them is very important so this error is saying that a network related error has occurred when trying to connect to the server so that means something is wrong with my connection string and i think i know what it is it's the double slash because we have to use the double slash in right in the connection string in the literal context here but then instead of the package manager i don't need that double slash so i'm going to remove it and then i'm going to try that again and this time i have no error i got the same warning but i have no error and if you take a look behind my package manager console and inside the project you're going to see a bunch of files appearing all right so let us take a look at what each one of them represents so i'm just going to collapse anything that's not absolutely necessary and then let's take a look at the context so it generated a context file the same context file that we created manually it just auto generated it it gave it the name based on the database it gave it the inheritance db context it has two constructors one parameter list one with the parameter accepting options from somewhere from some other application that's fine we have our db sets both created as virtual and then we have the same unconfiguring method right so if i did a side by side comparison you kind of see that while my mind is much more or ours is much more simplified it's really the same thing right we have the unconfiguring and then it says if there are no options configured then it puts in that connection string just like we did however it does give us this warning which is the same warning that we saw in the package manager console is just saying that you want to avoid putting a connection string directly inside of the context like this and like i said we're doing this provisionally because it's only a console app so we're just putting it right there just to get it working right but in a bigger application it would live in another file and be passed on demand you'll also take note of another method which we haven't quite looked at yet but like i said you can get to override most if not all of the methods that come standard from the db context class and this other method is on model creating so this one is basically just saying that we have the note annotation the relational collision setting the character sets that we're going to be using for this database and then it goes on to say model builder dot entity team has index and it has one league with many teams and has a foreign so remember that just by following the naming convention when we were creating the database it kind of implied all of this for us or did it in the migration in this situation it's looking at an existing database and then it is trying to formulate the rules in creating this existing database or interacting it with it that need to be adhered to all right in the same way if we were to change this database name and then tell it to do a migration it would actually look at all these rules to know how to do a migration for a brand new database that looks identical to the one it's scaffolded so it's a very i'm telling you ef core is very very intelligent and it's doing a lot it's pulling a lot of strings in the background for you all right so that is what the db context really looks like and then if we take a look at our classes that were generated you see that we get a partial class we get a hash set that represents the collection of teams so i'll get to that in a minute but we get that construct and we get the same properties and if we look in team it looks virtually just like how we did our own so that means we're on the right track right we still have that foreign key and the navigational property towards the league which is also virtual now just backtracking to what the league class has we see here that we have an additional property that we didn't put in our own so i'm just going to bring up the original league this is the one we created right and then the one that was scaffolded has this additional property which is a collection of teams now the concept here is that it's one too many we know that we we created it and we'll go over one-to-many later on but i just want to highlight that what it is doing here is it is saying that one league will have access to many teams one team belongs to one league but a league has many teams that's the one to many so with this navigational property set to be a collection or it could have been a list or well it used i collection by default when we were creating we could have said list we could have said innumerable but the point is that it is the collection of related items to this entity so just by getting one league i could include all of the teams and that would save me the the heartache of having to do two separate uh queries sorry to get the teams and the league that they're related to all right so it's very powerful stuff doing it like this so we can later on add that navigational property to our own but i just wanted to highlight what exactly was being scaffolded and what exactly is being done and pretty much that's it to scaffold in the database there are other little things that probably you can do but at the most basic level if there's a database maybe you started building third in sql server or it's a part of a legacy project or a legacy system and you want to rework it you just want to start working on it without starting from scratch then you can use this command bring in that database in the form of these models and then through the context you can just start working with it out of the gate
Info
Channel: Trevoir Williams
Views: 15,912
Rating: undefined out of 5
Keywords: .net core, .net core tutorial, Entity Framework Core | Trevoir Williams, Scaffold Existing Database - Entity Framework Core | Trevoir Williams, Trevoir Williams, asp.net core, blazor, dotnet core, ef core 5, ef core database first, ef core tutorial, entity framework, entity framework c#, entity framework core, entity framework tutorial, scaffold database, scaffold database command ef core, sofwaredevelopment, sql server, tutorial
Id: lrKnEDzhRiE
Channel Id: undefined
Length: 10min 25sec (625 seconds)
Published: Mon Jun 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.