Working on a project and exploring Database evolution from code first to database first and back to code first. The original project started with EF 6 the team struggled with that as there was no experts on the team and we found that there were some limitations around relationships that were getting lost in the code. So they migrated the EF 6 to a database project and at some point people started updated the Database Directly and doing schema compares to get the diffs back into the code. Not a good flow in my opinion, as if you are a squirrel hunter looking for shiny objects and forget to compare you potentially impact the whole team.
In comes EF Core 2.0, and wow super simple way of taking an existing Database and bringing over at least the structure back to code first. The seeding of the data is currently not included in EF Core 2.0 but it is possible and I will walk you through that in a separate post.
So here are the steps I went through in this example I will use Northwind Samples
- Create a new dotnet Core MVC application
- Install the tools for EF Core 2.0
- Scaffold the database with
- Create a Migration
- Change the connection string
- Add the migration
- Update the database
- Seed some tables - See next post
Create a new dotnet core MVC app
Dotnet core command line tools have built in templates
First up Scaffolding the database there is a command now called Scaffold-DbContext that does a great deal of the heavy lifting for you, creates your POCO (plain old code objects) and dbcontext along with any relationships, indexes etc...