Reputation: 3491
I'm wondering if there's a standard way of handling migrations when using EF Core database-first.
Every time I make change to the database I run this in the CLI:
dotnet ef dbcontext scaffold "Data Source=(local);Initial Catalog=myCatalog;Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f
The problem with it overrides every change I made in other entity and in the dbcontext, for example I have an interface of Id
for each entity, when I scaffold I later need to add this to the entity again, is it possible sync the project with the database or maybe just the specific entity?
Another problem is that I recently had to change the defaultDeleteBehavior
of the entities from DeleteBehavior.ClientSetNull
to DeleteBehavior.Cascade
so now every time I use the scaffolding approach I need to change manually 100+ entities in the OnModelCreating
method.
Upvotes: 5
Views: 10933
Reputation: 2287
I know this is very late after initial post, however, I faced a situation where I had to take over a system that is 15 years old. I wanted to create an API for the system, and make use of EF. What I've done is I used scaffold
to create the models and Entity configurations. I then created a migration from that and in the DB added the Migration entry into the __EFMigrationsHistory
table. If the table does not exist, you can do an Update-Database
which will produce an error because the tables already exist, but it creates that table in the DB. From there I was able to create additional migrations for my table changes that I want to make. Then I canuse the old structures as well.
Upvotes: 1
Reputation: 12629
Yes, you can limit which tables are reverse engineered by specifying schemas and tables.
The
--schema
option can be used to include every table within a schema, while--table
can be used to include specific tables. Note : Use--table
for CLI. And-table
for Package manager console.
Try like below to update only Foo
table. Click here for more detail.
dotnet ef dbcontext scaffold "Data Source=(local);Initial Catalog=myCatalog;Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f --table Foo
Upvotes: 3
Reputation: 7215
You don't really have database-first in .Net Core. So what that means is that the scaffold command is just there to get you up and running in a code-first project where you have an existing database.
What this means for you is that you have to decide where you're going to be making your changes:
a) The Database, or
b) The Data Access Project
Either of those options are valid, but you have to pick one and stick to it.
If you're making changes in the DB, you will have to ALTER all your tables and add the ID field to them, then run the Scaffold command again to update your model.
If you're making changes to your entities in the DA project, then you have to add an EF Core Migration each time you change a bunch of things.
In both those cases, you want to include the DB updates as part of your deployment, either via a bunch of SQL scripts, or by making the application perform a DB migration on Startup.
But what you should NOT be doing is trying to change things in both places. That's how you break deployments.
Upvotes: 8