I am using Clean Architecture web API in .NET 7. I need to employ the Database-First Approach because I am utilizing an existing database. However, I am creating the web API project from scratch. To do that, I also need to create ASP.NET Identity tables for user roles, and permissions in my application. So, do I need to manually create ASP.NET Identity tables as well, and scaffold tables like other tables? Or is there any way for Identity tables?
Previously, I was using the same structure but with a code-first approach. Now, I have to implement a database-first approach. Can you tell me what changes I am supposed to make?
2
Answers
Database-first is exactly that. All tables/foreign keys, indexes etc are created outside of Entity Framework. Your C# code will not contain migrations, seed data, or anything else that would modify the database structure.
Therefore, your database tables must be created by some other method. DbUp, SQL scripts,Database project, manually creating them, restoring from somewhere else, etc.
1.Use Db-first to restore context and models.
2.Use code-first (
Add-migration
update-database
) back to the database3.Change the generated context to inherit from
IdentityDbContext
4.Use code-first again to update the database with identity tables.
The idea is how to update the database only with the "identity tables". When you use
update-database
, it will check thedbo._EFMigrationHistory
tableIn this table, if it a migration file has been update to database once, the migration file will be recorded in this table. So next time upgrade, the recorded migration file will be skipped.