[MUSIC] >> Hi, friends. Welcome back to Entity Framework Core for beginners. In our previous video, I showed you how easy it is to use Entity Framework Core migrations to create and work
with a new database. First, we defined our
entity modeling code. Then we use migrations to change the database as we made
changes to the model. When we use that approach, we're treating the code as
the authoritative source of truth regarding the
shape of our entities. In this video, I'm going
to show you how to use Entity Framework Core to work with an existing database by
reverse engineering it. This approach treats the
database as the source of truth. Looking at Visual Studio, I have a brand new console app that doesn't have any code
added to it at all. I've already added the Microsoft.EntityFrameworkCore.Sql.Server.Design, and .Tools NuGetPackages. I have an existing database. Let's assume this
database was created and maintained by my organization's
database administrator. This database is already populated. To reverse engineer the database
and create my entity model code, I'll start by using the
Scaffold-DbContext command link. I'm going to pass in
the connection string, which can be found in the notes. Then I'll pass in the name
of the provider so it knows what type of database to execute
and connects the string against, and then I'll optionally specify output directories for the
DbContext and models classes. If you're using the .NET CLI, the command is dotnet
ef dbcontext scaffold. The parameters are pretty similar. Check the notes for more. Now that the scaffold is run, we have a complete
working entity model. Looking at the product entity, it should look pretty similar to the one we created
in the last video. One difference you'll note, however, is that there are no data annotations
describing the behavior of these properties like the one
we used previously on price. That's because these behaviors
are contained in the on model creating method of
the database context. This is another way EF
core lets you control the relationship between your
entities and the database. If we'd like to generate
an entity model that looks more like the one we
created in the previous video, we can do that too. I'll start by deleting
this entity model, and now I'll run the scaffold
DbContext command again. This time I'll add the
data annotation flag. Now, the product entity has data attributes that describe
the behavior of the properties. The on model creating method in the database context class
is also much more sparse. You might be wondering, what do we do when the
database schema changes? Well, there are two strategies. The first strategy is
a manual approach. This approach requires
you to manually edit your entity model to keep it in
sync with the database schema. The generated DbContext and model classes can be thought
of as a starting point for ongoing development similar to scaffolded razor pages in
an ASP.NET Core Web App. The other strategy is rescaffolding the entity model whenever
the database schema changes. Using this approach, it's important
to use partial classes or extension methods to keep business logic separate from
the scaffolded entities. This ensures that business
logic doesn't get overwritten if you rescaffold the
entities. Let's take a look. I'm going to delete my
entity model and rescaffold. This time, I'm going to generate my models in a subdirectory
of the models directory. I'll also specify the namespaces for the generated DbContext and models. Now, I can create partial classes in the models directory to
contain my business logic. This way, I can regenerate the entity models without
disturbing my business logic. Let's pay some code into program.CS so we can see the
partial class in action. Now that we've seen how Entity
Framework Core can work with an existing database,
in the next video, I'm going to show you how to
use Entity Framework Core with ASP.NET Core to streamline
your web development. [MUSIC]