I want to add a table that already exists in my SQL Server database to a DbContext
that I have so far only used for code-first migrations using Entity Framework Core 2.0.
Is there a way for me to create a model class, add a DbSet<T>
to the DbContext
descendant and have it "hook-up" with the table in the database without messing future migrations if I add or modify the current model.
I would rather not have to roll-back all migrations.
For EF Core 2, use scaffold
like dotnet ef dbcontext scaffold ... -t Sales.Orders -t Sales.OrderLines
See Shawn Wildermuth's article Reverse Engineer Databases Using EF Core 2
I believe you are looking for the option -IgnoreChanges
of the EF command Add-Migration
as detailed in Microsoft's Entity Framework Code First Migrations with an existing database (also: review the screen casts)
NOTE: Iterating until it is perfected is done by simply deleting the last migration, or running Remove-Migration
, making your changes, and then re-running the Add-Migration
until you have what you want.
See also Microsoft's article Migrations