'Synchronize single database between EF code first client and and EF database first client apps
Some time ago we started an ETL project which was relatively simple. It was reading source data, transforming it and loading it into a structured, relational database. In the very beginning of the project (when there was no target DB yet) we decided to use EF Core in order to manage schema changes (migrations). It was kind of a good candidate back then and we decided to continue with it.
But, recently we decided to develop a web application. An application which is supposed to read from that existing DB. As we have a lot of entities inside of it (tables), relationships between them and constraints, we decided to reach out to EF Core once again, so it can help us with the generation of the server-side code. As the database was already there, we had no other choice but to try the database first approach.
As described in most of the articles on the internet, we made a reference to the following nuget packages:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
So, with those you have an easy access to Sql servers and you can run commands such as:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
to get your entities and relations between them in your source code. So, the logical question was: How do I make sure that I can always have the latest schema representation in my ORM code?. Obviously, there's no such thing as Update-Database if we were using code first approach. Another option was to force the scaffolding to overwrite everything that I currently have in the repo:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force
but this has some issues such as if you deleted a table (entity) it will still stay in your source code and you will have to manually delete it. So, my question here is what would be the best approach for my use-case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

