Page 1 of 1

relation "__MigrationHistory" already exists error when attempting to use EF Code First with PostgreSQL

Posted: Fri 07 Sep 2012 21:39
by ibond
This problem happens even when using the CrmDemo located here: http://blogs.devart.com/dotconnect/enti ... irstsample

When running with EF 4.1 everything seems to work fine. When using EF 4.4 or 5.0 (upgraded using NuGet, I have to fix the references and using statements so the demo compiles) I get the given error after I run the program once: relation "__MigrationHistory" already exists.

The first time I run when the DB is empty everything appears to work fine and the __MigrationHistory table is created. After that I get the error every time. Other than upgrading Entity Framework and adding the necessary using directive, I have made no changes. I get the same problems in the my test projects.

I am using VS 2010 and PostgreSQL 9.1.5.

Any recommendations to fix this issue?

Re: relation "__MigrationHistory" already exists error when attempting to use EF Code First with PostgreSQL

Posted: Tue 18 Sep 2012 11:04
by Shalex
Thank you for your report. We have reproduced and are investigating the issue. As a temporary workaround, please turn off the database initialization strategy, i.e. comment the following line in the MyPgSqlContext.cs file:

Code: Select all

System.Data.Entity.Database.SetInitializer<MyPgSqlContext>(new MyDbContextDropCreateDatabaseAlways());
We will post here when the issue is fixed.

Re: relation "__MigrationHistory" already exists error when attempting to use EF Code First with PostgreSQL

Posted: Fri 21 Sep 2012 17:02
by Shalex
Starting from EFv4.3, the behaviour was changed: Drop must clear/remove all content of the database. So the DropCreateDatabaseAlways and DropCreateDatabaseIfModelChanges strategies can be used only with DeleteDatabaseBehaviour which is equal to AllSchemaObjects or Schema:

Code: Select all

var config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
config.DatabaseScript.Schema.DeleteDatabaseBehaviour = DeleteDatabaseBehaviour.AllSchemaObjects;
OR

Code: Select all

var config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
config.DatabaseScript.Schema.DeleteDatabaseBehaviour = DeleteDatabaseBehaviour.Schema;
We will update our blog article correspondingly.