[英]Prefix tables with entity framework automatic code migrations
I started a new project C#, and I used the "enable-migrations" command in the package console window. 我启动了一个新项目C#,并在程序包控制台窗口中使用了“ enable-migrations”命令。 This naturally added migrations to my project.
这自然为我的项目增加了迁移。 I then set automatic migrations to true, so that as I call "update-database" it will create my tables for me with all keys and that.
然后,我将自动迁移设置为true,以便在我调用“ update-database”时它将为我创建带有所有键的表。
The only problem is that I have multiple websites where want to do this, which all use the ASP.NET membership provider to login. 唯一的问题是,我有多个要执行此操作的网站,这些网站都使用ASP.NET成员资格提供程序进行登录。 Which through automatic code migrations create a bunch of account tables for me to use.
通过自动代码迁移可以创建一堆帐户表供我使用。 But the tables are all called the same, so if I do this targeting the same database for different sites they will overwrite eachother.
但是这些表都被称为同一表,因此,如果我针对不同站点针对同一数据库执行此操作,则它们将相互覆盖。 So the question I got is this: How can I specify a prefix for my tables created by the entity framework?
所以我得到的问题是:如何为实体框架创建的表指定前缀?
I've seen several ideas on how to do this while searching, but they didn't work for me (the necessary properties wasn't there for some reason and so on.) 我在搜索时已经看到了一些有关如何执行此操作的想法,但是它们对我没有用(由于某些原因而没有必要的属性,依此类推。)
Thank you 谢谢
Xenoxsis Xenoxsis
I'm not sure how do you plan to do just that - if I'm getting it right you'd want to keep one database (shared) in between number of web sites
- yet, have each site has its own membership tables, named differently, with different prefixes, right? 我不确定您打算怎么做-如果我做对了,您想
one database (shared) in between number of web sites
保留one database (shared) in between number of web sites
-但是,每个网站都有自己的会员表,用不同的前缀命名,对吗?
First problem is that for each Db/table name change - you need a 'code to match'
- ie code first entities and code, the 'migration table' in the Db - and tables are all in sync - so it could all work together as it should. 第一个问题是,
for each Db/table name change - you need a 'code to match'
-即,对第一个实体和代码进行编码,Db中的“迁移表”-并且表都是同步的-因此它们可以一起工作正如它应该。 In that sense, just changing script or table names in Db won't work . 从这种意义上讲, 仅更改Db中的脚本或表名是行不通的 。
It has to be done at the level of attributes (as @Steven suggested) or fluent configuration.
Which in your case, it means that somehow you'd need to 'build' separate configurations for each site, deploy them separately (code) to each site - and build one mega Db
that contains all the small variants
of each merged together. 在您的情况下,这意味着您需要以某种方式为每个站点“构建”单独的配置,将它们分别(编码)部署到每个站点-并构建一个
mega Db
,其中包含每个合并在一起的所有小variants
。
That's going to be tough to manage - but you could try (what I described above) - I have no idea if it'd work (as this requires lot of 'infrastructure' to try this one) - but maybe along these lines... 这将很难管理-但是您可以尝试(如上所述)-我不知道它是否可行(因为这需要大量的“基础设施”才能尝试)-但也许可以遵循这些原则。 。
Table
attributes (or via fluent config) Table
属性(或通过流畅的配置) Update-Database -Script
to save the actual scripts for each case (important). Update-Database -Script
以保存每种情况的实际脚本(重要)。 merge
the migrations - scripts - into one big master script
- ie remove the identical set of tables (leave just one of course) - and copy all different sets for membership tables. merge
迁移-脚本- merge
为one big master script
-即删除相同的表集(当然只保留其中一个)-并复制成员表的所有不同集。 migration table
from the database - as that'd surely be out of sync and won't let you do anything (or there is also a flag in code I think to just ignore that, don't have it right now). migration table
-因为这肯定会不同步,并且不会让您做任何事情(或者代码中也有一个标志,我认为可以忽略它,暂时不要使用它)。 For details see below in my other post. There must be something smarter - but on the other hand, migrations are not made to work for such scenarios, so it's going to be hard if not impossible to pull this off. 必须有一些更聪明的东西-但是,另一方面,迁移不适用于此类情况,因此要实现这一目标将非常困难,即使不是不可能。
Some general info that might help... 一些一般信息可能会有所帮助...
How to synchronize migrations with existing databases - geared toward production scenarios, maintaining Db-s and CF to match. 如何使迁移与现有数据库同步 -针对生产场景,保持Db-s和CF匹配。 It's not exactly what you need but has a detailed description, possible ways to resolve this which I wrote a while ago...
这不是您真正需要的,但是有一个详细的描述,解决此问题的可能方法,这是我前一段时间写的...
MVC3 and Code First Migrations - "model backing the 'blah' context has changed since the database was created" MVC3和代码优先迁移-“自创建数据库以来,支持'blah'上下文的模型已更改”
To summarize... 总结一下...
What works for me is to use Update-Database -Script
对我有用的是使用Update-Database -Script
That creates a script with a 'migration difference', which you can manually apply as an SQL script on the target server database (and you should get the right migration table rows inserted etc.).
这将创建一个具有“迁移差异”的脚本,您可以将其作为SQL脚本手动应用到目标服务器数据库上(并且应该插入正确的迁移表行等)。
If that still doesn't work - you can still do two things...(more inside)...
如果那仍然不起作用-您仍然可以做两件事...(更多内容在里面...)
I don't know of anyway to make Entity Framework do this automatically across all entities. 无论如何,我都不知道要让Entity Framework在所有实体之间自动执行此操作。 But you could force a table name, or schema using attributes or fluent API to get the desired effect.
但是您可以使用属性或流利的API强制使用表名或模式来获得所需的效果。 For example:
例如:
[Table("[put prefix here]_Users", Schema = "[put schema here]")]
public class User {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.