简体   繁体   English

实体框架优先代码和现有数据库

[英]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有两个您将要熟悉的主要命令

  • 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 Update-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. 当添加,删除或更改实体类或更改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.

相关问题 实体框架代码优先使用现有数据库时的好处 - Entity Framework Code-First Benefits when using existing database 在现有MySQL数据库上使用实体框架4.1 Code-First - Using entity framework 4.1 Code-First on existing MySQL database 实体框架代码优先数据库未更新 - Entity Framework Code-First Database Not Updating 实体框架 5 代码优先不创建数据库 - Entity Framework 5 code-first not creating database 以编程方式创建连接字符串以将Entity Framework Code-First模型与现有Sql Azure联合数据库进行映射 - Programmatically creating a connection string for mapping an Entity Framework Code-First model with an existing Sql Azure federation database 首先更新Entity Framework 6中的现有实体 - Updating existing entities in Entity Framework 6 code-first 无法使用Entity Framework代码创建数据库 - 首先+没有数据库 - Can't create database with Entity Framework code-first + no database 无法使用实体框架代码优先使用凭据创建数据库? - Cannot Create Database with credentials using Entity Framework Code-First? 在代码优先实体框架中连接数据库时出错 - Error while connecting to database in code-first Entity Framework 使用Entity Framework 4和Code-First从数据库中排除字段/属性 - Exclude a field/property from the database with Entity Framework 4 & Code-First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM