简体   繁体   English

实体框架6数据库迁移,用于隔离的多租户设置

[英]Entity Framework 6 database migrations for isolated multi-tenant setup

I understand database migrations might not be the best method to deploy this solution, so any suggestions would be greatly appreciated. 我了解数据库迁移可能不是部署此解决方案的最佳方法,因此任何建议将不胜感激。

I have x identical database schemas, one per client. 我有x个相同的数据库架构,每个客户端一个。

I also have models for each one of these databases and am able to deploy a migration which creates a new clone. 我还为每个数据库都有模型,并且能够部署创建新克隆的迁移。 Up until this point, I had been using PHP and scripts to loop through all the schemas and update any changes to the structure. 到目前为止,我一直在使用PHP和脚本遍历所有模式并更新对结构的任何更改。 We are moving over to C# and EF6 due to Web API 2 and attribute routing. 由于Web API 2和属性路由,我们将移至C#和EF6。

My question is; 我的问题是; Is there a way to: 有没有办法:

  1. Deploy a build command which will do what update-database would do for a migration, and pass it a database name (in order to create a new clone schema of the account database? 部署一个build命令,该命令将执行update-database对于迁移的作用,并向其传递数据库名称(以便创建帐户数据库的新克隆模式?

  2. Deploy a build command which might do what update-database would do recursively through each one of the target databases? 部署一个构建命令,该命令可能会通过每个目标数据库来递归执行update-database操作?

"I have x identical database schemas, one per client " “我有x个相同的数据库模式,每个客户端一个”
and
"I also have models for each one of these databases" “我也为每个数据库都有模型”

Did you mean I also have models for each one of these Schemas? 您的意思是说我也为每个模式都有模型吗?

Ef model entities are linked to a Schema/TableName. EF模型实体链接到Schema / TableName。

entity.ToTable("tableName", "schemaName"); entity.ToTable(“ tableName”,“ schemaName”);

So if each client gets their own schema, then each model has all tables in that schema and the client gets their own model. 因此,如果每个客户端都有自己的架构,则每个模型都具有该架构中的所有表,并且客户端也会获得自己的模型。 So how do I run update-database on each Context model. 因此,如何在每个上下文模型上运行update-database。 I another way of looking at the issue. 我以另一种方式看待这个问题。

So the answer lies with how you are tracking the schema per client info. 因此,答案在于您如何跟踪每个客户端信息的架构。

Powershell migrate.exe approach might be interest for your so that you can trigger migration on many context models. 您可能会对Powershell migrate.exe方法感兴趣,因此可以在许多上下文模型上触发迁移。

Custom migration operations might also be interesting. 自定义迁移操作也可能很有趣。 Rowan is an EF developer. Rowan是EF开发人员。

EDIT: based on Auto Migration comment, this is worth a look Managing migration triggers in code 编辑:基于自动迁移评论,这是值得一试 管理迁移代码触发

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

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