Cannot Create/Migrate Databases using Entity Framework

Cannot Create/Migrate Databases using Entity Framework

Postby brad.bow » Tue 19 Apr 2016 06:42

Hi,

We are considering using dotConnect for PostgreSQL. We are using entity framework 6 and are primarily interested in full text search capability outlined in this blog post: http://blog.devart.com/using-postgresql-full-text-search-in-entity-framework.html

We are having issues with creating and migrating databases using your provider.

We have the following simple db context:

Code: Select all
namespace DevartConnection
{
    public class DevartDbContext : DbContext
    {
        public virtual DbSet<User> Users { get; set; }

        public DevartDbContext(DbConnection connection, bool contextOwnsConnection = true)
            : base(connection, contextOwnsConnection)
        {
            Database.SetInitializer(new CreateDatabaseIfNotExists<DevartDbContext>());
        }

        public DevartDbContext()
        {
           
        }

    }

    public class User
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
    }
}


And then a simple console application than manually creates a connection and attempts to use it with the db context. Given no database exists the first attempt to add entities should create the database.

Code: Select all
namespace DevartConnection
{
    class Program
    {
        static void Main(string[] args)
        {
            PgSqlConnection connection = new PgSqlConnection
            {
                Host = "[our host]",
                Port = 5433,
                Database = "dbthatdoesntexist",
                UserId = "[our user id]",
                Password = "[our password]",
                Schema = "dbo"
            };
           
            Console.WriteLine("Connecting to database");
            using (var dbContext = new DevartDbContext(connection))
            {
                dbContext.Users.Add(new User()
                {
                    Id = 1,
                    Name = "Brad Bow"
                });
                dbContext.SaveChanges();
            }
            Console.WriteLine("Successfullly saved item");
            Console.ReadLine();
        }
    }
}


However, we get a System.Data.Enitity.Core.ProviderIncompatibleException. This has an inner exception that claims "The provider did not return a ProviderManifestToken string.", which in turn has an inner exception that claims "{"database \"dbthatdoesntexist\" does not exist"}.

Here is the app.config for the console application.

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Devart.Data.PostgreSql" />
      <add name="dotConnect for PostgreSQL"
           invariant="Devart.Data.PostgreSql"
           description="Devart dotConnect for PostgreSQL"
           type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql, Version=7.4.616.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <providers>
      <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices,
      Devart.Data.PostgreSql.Entity, Version=7.4.616.6, Culture=neutral, PublicKeyToken=09af7300eec23701" />
    </providers>
  </entityFramework>
</configuration>


We have confirmed that we can connect to an existing database and query data (on the same host, port, using the same user id and password). So the connection is definitely not the problem.

We have added the Devart.Data.PostgreSql.Entity.Migrations dll from C:\Program Files (x86)\Devart\dotConnect\PostgreSQL\Entity\EF5\Devart.Data.PostgreSql.Entity.Migrations.dll as a reference to our console project. We couldn't see an equivalent dll in the EF6 folder, is there supposed to be one there?

Any ideas?

Thanks
Brad
brad.bow
 
Posts: 1
Joined: Tue 19 Apr 2016 06:22

Re: Cannot Create/Migrate Databases using Entity Framework

Postby Shalex » Tue 19 Apr 2016 09:20

brad.bow wrote:We have added the Devart.Data.PostgreSql.Entity.Migrations dll from C:\Program Files (x86)\Devart\dotConnect\PostgreSQL\Entity\EF5\Devart.Data.PostgreSql.Entity.Migrations.dll as a reference to our console project. We couldn't see an equivalent dll in the EF6 folder, is there supposed to be one there?
With the release of Entity Framework 6 there is no more need to place Code-First Migrations functionality in a separate assembly. Code-First Migrations functionality is integrated to C:\Program Files (x86)\Devart\dotConnect\PostgreSQL\Entity\EF6\Devart.Data.PostgreSql.Entity.dll.

Refer to
http://blog.devart.com/entity-framework-6-support-for-oracle-mysql-postgresql-sqlite-and-salesforce.html
http://blog.devart.com/entity-framework-code-first-migrations-support-for-oracle-mysql-postgresql-and-sqlite.html

brad.bow wrote:inner exception that claims "{"database \"dbthatdoesntexist\" does not exist"}
Please set the following option:
Code: Select all
var config = Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfig.Instance;
config.DatabaseScript.Schema.DeleteDatabaseBehaviour = Devart.Data.PostgreSql.Entity.Configuration.DeleteDatabaseBehaviour.Database;
For more information, refer to https://www.devart.com/dotconnect/postgresql/docs/?DBScripGeneration.html.

JIC
Entity Framework engine has two mechanisms for creating database objects in EF: the old Code-First CreateDatabase() and the new Code-First Migrations. The [Index] attribute only affects the Code-First Migrations (code-based, automatic). However, the [Index] attribute will be taken into attention if the database initialization strategy MigrateDatabaseToLatestVersion is specified.
Shalex
Devart Team
 
Posts: 7774
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for PostgreSQL