简体   繁体   English

实体框架6更新数据库因使用-IgnoreChanges创建的迁移而失败

[英]Entity Framework 6 Update-Database fails for migration created with -IgnoreChanges

I'm trying to create a DbContext specific to may Domain that has one model class entirely managed by EF, and another that gets mapped to a database view. 我正在尝试创建一个特定于may Domain的DbContext,它具有一个完全由EF管理的模型类,而另一个则映射到数据库视图。 To ensure EF doesn't try to create this view that already exists, I added just the view to my DbContext and ran the following command: 为了确保EF不会尝试创建已经存在的视图,我仅将该视图添加到了DbContext并运行了以下命令:

Add-Migration InitialUnmanaged -IngoreChanges

This creates a Migration with an empty Up and Down, which is exactly what I want. 这将创建一个带有空的Up和Down的迁移,这正是我想要的。 I'm able to update my dev database using this Migration, but whenever I try to add a second Migration that includes my EF-managed model class, I get an error. 我可以使用此Migration更新我的dev数据库,但是每当我尝试添加包含EF管理的模型类的第二个Migration时,都会出现错误。

Add-Migration Initial

Unable to generate an explicit migration because the following explicit migrations are pending: [201510151553565_InitialUnmanaged]. 无法进行显式迁移,因为以下显式迁移正在处理中:[201510151553565_InitialUnmanaged]。 Apply the pending explicit migrations before attempting to generate a new explicit migration. 在尝试生成新的显式迁移之前,请应用未决的显式迁移。

The thing is, I've already applied that explicit Migration, and I can see it in my MigrationHistory table. 问题是,我已经应用了显式迁移,并且可以在“迁移历史记录”表中看到它。

MigrationId 迁移编号

201510151553565_InitialUnmanaged 201510151553565_Initial不受管理

Can anyone help me understand why this is happening and how to work around it? 谁能帮助我了解为什么会这样以及如何解决?

I figured this out. 我想通了。

When I applied the Migration, it put the Migration History table in the same default schema as the model. 当我应用迁移时,它将“迁移历史记录”表置于与模型相同的默认架构中。 However, when I tried to roll it back, it was looking for the Migration History in the connected user's schema. 但是,当我尝试回滚时,它正在连接的用户的架构中寻找迁移历史记录。 Since it wasn't there, it thought the database was at version 0. 由于它不存在,因此它认为数据库的版本为0。

I created an OracleDbConfiguration class to specify a custom HistoryContext that specifies the default schema for the Migration History table and I'm able to rollback as expected after applying a Migration. 我创建了一个OracleDbConfiguration类来指定一个自定义的HistoryContext,它为“迁移历史记录”表指定了默认架构,并且可以在应用“迁移”后按预期进行回滚。

I verified that I was able to recreate my initial steps and have them work as expected now that I'm explicitly specifying the schema for the Migration History table. 我已经确认可以重新创建初始步骤,并使它们能够按预期工作,因为我已经明确指定了“迁移历史记录”表的架构。

Either delete the migration or just run Update-Database to put entity back in sync. 删除迁移或只运行Update-Database以使实体恢复同步。 If it is an empty migration your database will not change. 如果是空迁移,则数据库将不会更改。

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

相关问题 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration 实体框架要求在已附加迁移的情况下更新数据库 - Entity Framework asks to update-database when migration is already appended 实体框架核心更新-数据库特定迁移 - Entity framework Core Update-database specific migration 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE 实体框架6中“更新数据库”之后发生错误 - Error after 'update-database' in Entity Framework 6 实体框架核心 - “更新数据库”不起作用 - Entity Framework Core - 'Update-Database' not working 带有-ConnectionString的实体框架更新数据库 - Entity Framework Update-Database with -ConnectionString 实体框架核心更新-数据库特定的迁移没有权限错误 - Entity Framework Core Update-database specific migration gives no permission error 实体框架-迁移更新数据库引发奇怪的错误 - Entity Framework - Migrations update-database Throwing strange error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM