简体   繁体   English

使用实体框架更改持续交付和数据库架构

[英]Continuous delivery and database schema changes with entity framework

We want to progress towards being able to do continuous delivery of of our application into production. 我们希望能够将我们的应用程序连续交付到生产中。 We currently deploy to azure and use table/blob storage and have a azure sql database, which we access with the entity. 我们目前部署到azure并使用table / blob存储并拥有一个azure sql数据库,我们可以使用该实体访问它。

As the database schema changes we want to be able to automatically apply the schema changes to the production database, but as this will happen whilst the application is live and the code changes are being deployed to many nodes at the same time we are not sure what the correct approach is. 随着数据库模式的更改,我们希望能够自动将模式更改应用到生产数据库,但是当应用程序处于活动状态并且代码更改同时部署到许多节点时,我们不确定是什么正确的方法是。

After some reading it seems (and this makes sense) that the application needs to be tolerant of the 2 different database schema versions, so that it doesn't matter if its an old version of the code or a new version of the code which sees the database, however I'm not sure what the best way to approach handling this in the application is, using the entity framework. 经过一些阅读后,似乎(这是有道理的)应用程序需要容忍2个不同的数据库模式版本,因此无论是旧版本的代码还是新版本的代码都无关紧要数据库,但是我不确定在应用程序中处理这个问题的最佳方法是使用实​​体框架。

Should we have versioned instances of the EF generated classes in the code which know how to access a specific version of the schema? 我们是否应该在代码中具有EF生成的类的版本化实例,这些实例知道如何访问特定版本的模式? What happens when the schema is updated and an old version of the code is running against the database? 更新架构并且针对数据库运行旧版本的代码时会发生什么?

Our entity framework classes are mapped to views in specific schemas in the db and nothing is mapped to the underlying tables, so potentially this could allow us to create v1 views which the old code uses and v2 views which the new code uses, but maintaining this feels like it would be a bit of a nightmare (its already enough of a pain simply maintaining the EF mappings to views rather than tables) 我们的实体框架类映射到db中特定模式中的视图,并且没有任何内容映射到底层表,因此可能允许我们创建旧代码使用的v1视图和新代码使用的v2视图,但保留此感觉它会有点像一场噩梦(它已经足够痛苦只是维持EF映射到视图而不是表格)

So what are best practices in this area? 那么这个领域的最佳实践是什么? What do others do to solve this problem? 别人怎么做才能解决这个问题?

Whether you use EF or not, maintaining the code's ability to work with 2 consecutive versions of the database is a good (and perhaps the only viable) approach here. 无论您是否使用EF,维护代码能够使用2个连续版本的数据库都是一种很好的(也许是唯一可行的)方法。

Here are some ways we handle specific types of migrations: 以下是我们处理特定迁移类型的一些方法:

  • When adding a column, we can typically just add the column (with a default constraint if non-nullable) and not worry about the code. 添加列时,我们通常只需添加列(如果不可为空,则使用默认约束),而不必担心代码。 EF will never issue a "SELECT *", so it will be able to continue to function properly while ignoring the new column. EF将永远不会发出“SELECT *”,因此它将能够在忽略新列的情况下继续正常运行。 Similarly, adding a table is easy. 同样,添加表格也很容易。

  • When removing a column or table, simply keep that column around 1 version longer than you would have otherwise. 删除列或表时,只需将该列保留的版本比其他版本长1个版本。

  • For more complex migrations (eg completely changing the structure for a table or segment of the data model), deploy the new model alongside backwards-compatibility views (or tables with triggers to keep them in-sync), which will live as long as does the code that references them. 对于更复杂的迁移(例如,完全更改数据模型的表或段的结构),将新模型与向后兼容性视图(或具有触发器的表保持同步)一起部署,这将持续生存引用它们的代码。 As you say, this can a lot of work depending on the complexity of the migration, but it sounds like you are already well-positioned to do this because your EF entities point to views anyway. 正如你所说,这可以做很多工作,具体取决于迁移的复杂性,但听起来你已经做好了准备,因为你的EF实体无论如何都指向了视图。 On the other hand, the benefit of this work is that you have more time to do the code migration. 另一方面,这项工作的好处是您有更多时间进行代码迁移。 If you have a large codebase, this could be really beneficial in allowing you to migrate the data model to fit the needs of new features while still supporting old features without major code changes. 如果您拥有一个庞大的代码库,这可能非常有利于您迁移数据模型以满足新功能的需求,同时仍然支持旧功能而无需重大代码更改。

As a side-note, the difficulty of data migration often makes us push developing a finalized data model as far back as possible in the development schedule. 作为旁注,数据迁移的难度常常使我们在开发计划中尽可能地推动开发最终的数据模型。 With EF, you can write and test a lot of code before the data model is finalized (we use code-first to generate a sample SQLExpress database in a unit tests, even though our production database is not maintained by code-first). 使用EF,您可以在完成数据模型之前编写和测试大量代码(我们使用代码优先在单元测试中生成示例SQLExpress数据库,即使我们的生产数据库不是通过代码优先维护的)。 That way, we make fewer incremental changes to the production data model once a new feature is released. 这样,一旦发布新功能,我们对生产数据模型的增量更改就会减少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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