0
votes

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. 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:

Add-Migration InitialUnmanaged -IngoreChanges

This creates a Migration with an empty Up and Down, which is exactly what I want. 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.

Add-Migration Initial

Unable to generate an explicit migration because the following explicit migrations are pending: [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

Can anyone help me understand why this is happening and how to work around it?

2
Try deleting the migration from the table and running the first migration againAlex Krupka
Did you run update-database? That will change the model on the db side.Steve Greene
There's something very strange going on. When I reset everything and make my initial Migration include everything, I can update the database just fine, but then it fails when I try to roll that back. Update-Database -target:0 gives an error saying Target database is already at version 0.Matt Knowles
@SteveGreene, yes. I'm able to update my dev database using this Migration, but whenever I try to add a second Migration, I get the error.Matt Knowles
Try updating with an explicit connection string [-ConnectionStringName <String>]Steve Greene

2 Answers

2
votes

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.

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.

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.

0
votes

Either delete the migration or just run Update-Database to put entity back in sync. If it is an empty migration your database will not change.