[英]Entity Framework code-first and existing database
I'm working on an application with asp.net mvc that supports install, remove plugins. 我正在使用支持安装,删除插件的asp.net mvc开发应用程序。
When I want to install a new plugin I have an Install method that registers new routes and ... 当我想安装一个新插件时,我有一个Install方法来注册新路由并...
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 Code First Migrations有两个您将要熟悉的主要命令
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.
当添加,删除或更改实体类或更改DbContext类时,下次运行该应用程序时,它将自动删除现有数据库,创建一个与模型匹配的新数据库,并将其与测试数据一起播种。 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.
Code First迁移功能通过启用Code First更新数据库架构而不是删除并重新创建数据库来解决此问题。
I recommend to have look following link which makes you more clear about your problem. 我建议您查看以下链接,该链接可以使您更清楚地了解自己的问题。
https://msdn.microsoft.com/en-us/data/jj591621 https://msdn.microsoft.com/zh-CN/data/jj591621
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.