简体   繁体   English

EF代码首先从Nuget参考迁移

[英]EF code first migration from Nuget reference

Closest thing I could find was this question: EF code migration in reference dll coming through nuget 我能找到的最接近的问题是: 通过nuget的参考dll中的EF代码迁移

Similar to that post, we have a package that uses EF 6 to write some cross-reference records. 与该帖子类似,我们有一个使用EF 6编写一些交叉引用记录的程序包。 I recently modified the package which modified the schema of the table used by EF. 我最近修改了该软件包,该软件包修改了EF使用的表的架构。 Added the migration, etc, and re-deployed the Package. 添加了迁移等操作,然后重新部署了软件包。 All good so far. 到目前为止一切都很好。

I've referenced this Nuget package in two different solutions. 我在两个不同的解决方案中引用了此Nuget程序包。 In one solution, the package did not exist yet so I added it, built, deployed (both using Team City) to Azure. 在一个解决方案中,该软件包尚不存在,因此我将其添加,构建,部署(均使用Team City)到Azure。 For that solution, there were no pre-existing migrations and therefor no __MigrationHistory table. 对于该解决方案,没有预先存在的迁移,因此没有__MigrationHistory表。 Once the code hit the xref service it happily created the table and the __MigrationHistory. 一旦代码到达外部参照服务,它就会愉快地创建表和__MigrationHistory。 The table and history all have the correct columns and the full migration history... So far, so good. 该表和历史记录都具有正确的列和完整的迁移历史记录...到目前为止,很好。

In the second solution, the package already existed so I simply updated it. 在第二个解决方案中,该软件包已经存在,因此我只对其进行了更新。 What I expected to happen is when this code called the xref service it would also run the migration, update the column name, and insert the __MigrationHistory record. 我期望发生的是,当此代码称为外部参照服务时,它还将运行迁移,更新列名并插入__MigrationHistory记录。 But, alas, all I get in my logs is "The model backing the 'DatabaseDef' context has changed since the database was created..." 但是,可惜,我在日志中得到的只是“自创建数据库以来,支持'DatabaseDef'上下文的模型已更改...”

Nowhere in either solution do we use any SetInitializer (code nor config). 在任何一种解决方案中,我们都不使用任何SetInitializer(代码或配置)。 Both solutions have references to EF in the same places. 两种解决方案都在同一位置引用了EF。 The only difference is that for the "one that worked" I was actually running through the debugger when it hit the xref service code. 唯一的区别是,对于“有效的”,我实际上是在调试器到达外部参照服务代码时通过调试器运行的。 I have not tried the other solution running via debugger and cannot think why that would matter, but stranger things have happened. 我没有尝试过通过调试器运行的其他解决方案,也无法考虑为什么这很重要,但是发生了一些奇怪的事情。 I also have a request in with our Ops team to see if the Team City deployment turns on the code-first migrations during deployment in some instances. 我还要求我们的Ops团队查看Team City部署是否在某些情况下在部署期间打开了代码优先迁移。

Questions: 问题:

  1. Any ideas why one would work and not the other? 有任何想法为什么一个可行,而不是另一个可行?
  2. in the "Xref Nuget Package", where would I put an initializer (if at all)? 在“外部参照Nuget程序包”中,我将在哪里放置初始化程序(如果有的话)? There is no App_Start or global.asax in there... just an assembly doing its' thing to write the xref records. 那里没有App_Start或global.asax……只是一个程序集正在做它的事情来写外部参照记录。
  3. Anything else I'm overlooking? 我还有什么要注意的吗?

The error says that your datacontext schema and database schema have some conflicts. 该错误表明您的数据上下文架构和数据库架构存在一些冲突。 If you sure that both are same, run add-migration, delete all codes from the "UP()" method. 如果确定两者相同,请运行add-migration,然后从“ UP()”方法中删除所有代码。 run "update-database". 运行“更新数据库”。

refer this thread: Entity Framework Code Migrations - Stuck on Initial Migration 请参阅此线程: 实体框架代码迁移-卡在初始迁移上

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

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