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

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
ibond
Posts: 1
Joined: Fri 07 Sep 2012 21:28

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

Post by ibond » Fri 07 Sep 2012 21:39

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?

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Tue 18 Sep 2012 11:04

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.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

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

Post by Shalex » Fri 21 Sep 2012 17:02

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.

Post Reply