How to combine both of code first and database first approaches

ぃ、小莉子 提交于 2019-12-02 04:50:48

1) Reverse engineer the models and db context from the existing database. Follow this link for that, https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

2) Now you have the models and db context. Your data access should work fine with the new context, except migrations.

3) Now that you already have the database and tables created, you need to tweak your DB to pretend that current schema is created using migrations.

3.1) First create the Initial Migration Add-Migration IntitialCreate. It will have code to create the database up to current level.

3.2) Now you should have to manually create __MigrationHistory table and manually insert IntialMigration to the table, so that it wont try to apply it again. If you find hard to do this manually, there is trick.

Create a new database using you current migrations by just changing the Database name in the connection string and executing update-database. In the new database created __MigrationHistory table should be created with InitialCreate migration already applied. Generate a create script along with data and execute that in the original database.

4) Now you can do the rest of the changes to the model, create new migrations apply them as usual.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!