简体   繁体   中英

Entity Framework code-first and existing database

I'm working on an application with asp.net mvc that supports install, remove plugins.

When I want to install a new plugin I have an Install method that registers new routes and ...

For database, I use a code-first approach for creating database and every plugin has it's own context class.

My question is: when I want to install a new plugin, I need to create additional tables in my existing database, or create a new database if the database does not yet exist. And if those tables are already there, nothing should be created.

How do I achieve this?

Thanks in advance

Code First Migrations has two primary commands that you are going to become familiar with

  • Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
  • Update-Database will apply any pending migrations to the database

When you develop a new application, your data model changes frequently, and each time the model changes, it gets out of sync with the database. You have configured the Entity Framework to automatically drop and re-create the database each time you change the data model. When you add, remove, or change entity classes or change your DbContext class, the next time you run the application it automatically deletes your existing database, creates a new one that matches the model, and seeds it with test data. This method of keeping the database in sync with the data model works well until you deploy the application to production. When the application is running in production it is usually storing data that you want to keep, and you don't want to lose everything each time you make a change such as adding a new column. The Code First Migrations feature solves this problem by enabling Code First to update the database schema instead of dropping and re-creating the database.

I recommend to have look following link which makes you more clear about your problem.

https://msdn.microsoft.com/en-us/data/jj591621

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM