Learn how to create the EF model based on the existing database. This video shows you how to run a Scaffold-DbContext command to create a model from the existing database. The reverse-engineer process creates entity classes and a derived context based on the schema of the existing database. The entity classes are simple C# objects that represent the data you will be querying and saving.
- [Voiceover] Okay, so we are now ready to create the Entity Framework model based on our existing database. And what we're going to do is enter in a command in the package manager console, and I already have it in my buffer, so I will go ahead and paste that in, but let me go ahead and also paste it in Notepad and walk us through exactly what is there. So, the command we're going to be using is Scaffold Db Context, so it's going to be generating the Db Context, in addition to another class file, so what we need to do is follow that command by a space and within quotes, essentially have the same contents of a connection string, where we specify our server, the instance, followed by the database, which in our case is Actor Db, then we indicate that trusted connection equals true, and that ends the quotes, and we follow it by a space and indicate Microsoft dot Entity Framework core dot SQL server, and then specify the output directory, which is going to be in models.
Okay, so that's the content within this package manager console, and now I will hit enter to try to execute it, alright. Looks like I get an error here, says could not load file or assembly, Microsoft dot extensions logging. Let's look at the project dot JSON. And I do see here on line 20, there's this green squiggly line that says that there's a dependency conflict. It's expecting 1.1, so let's go ahead and change that to 1.1, save that, let it restore.
Looks like the line went away. I will build a solution, alright, looks like it succeeded. And now, back in the package manager console, I'll just hit the up arrow to re enter the last command that I dropped in there. And let's hit enter to execute it and see what happens. Okay, looks good, a couple of things happened. This reverse engineering process created an entity class and derived the context based class on the schema of the existing database.
So here's the models folder, there's our Actor's class that it created, and right above it, we see our Actor Db context class that's based on the schema of the existing database, which is pretty cool. Very similar to what we created when we were putting together our console application. And right here, this long string is basically just a comment indicating that this connection string really should be in a different location.
But by default, that's where it dropped it. This context represents a session with the database and allows us to query and save instances of the entity classes. Okay, so the next thing we want to do is register our context with the pennancy injection. That's up next.
- Entity Framework (EF) overview
- Uses for EF
- Installing EF
- Creating entities
- Creating the DbContext
- Performing migrations
- Creating an MVC application
- Installing EF packages
- Reverse engineering the model
- Performing context and dependency injection
- Creating the controller and the view
- Using SqlServerTypeMapper
Skill Level Intermediate
Q: This course was updated on 06/23/2017. What changed?
A: The following topics were updated: using the exercise files; Mac install prerequisites; Entity Framework on Mac, part 1; and Entity Framework on Mac, part 2.