Add pre-existing table to code-first migration in EF Core 2.0

.net-core c# entity-framework entity-framework-core entity-framework-core-migrations

Question

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.

1
1
3/6/2018 1:58:33 AM

Popular Answer

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

0
3/6/2018 3:31:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow