Code-First CreateDatabase supported?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
dresel
Posts: 18
Joined: Tue 20 Nov 2012 13:38

Code-First CreateDatabase supported?

Post by dresel » Tue 24 Jun 2014 13:04

Hi,

I'm trying to setup Code-First with dotConnect for PostgreSQL. I want the database "lacowiki" to be created if none exist.

When using

Code: Select all

PortalContext context = new PortalContext();
context.Database.Create();
I get an "database "lacowiki" does not exist" exception.

Should this be supported?

When the database exists, seeding or initialization strategies like DropCreateDatabaseAlways / DropCreateDatabaseIfModelChanges are working though...

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

Re: Code-First CreateDatabase supported?

Post by Shalex » Tue 24 Jun 2014 16:38

Please refer to http://www.devart.com/dotconnect/postgr ... ation.html. If this doesn't help, specify your current DeleteDatabaseBehaviour value.

dresel
Posts: 18
Joined: Tue 20 Nov 2012 13:38

Re: Code-First CreateDatabase supported?

Post by dresel » Wed 25 Jun 2014 14:02

I modified the configuration to

Code: Select all

<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" />
	<!-- Devart specific configuration section -->
	<section name="Devart.Data.PostgreSql.Entity" type="Devart.Data.PostgreSql.Entity.Configuration.PgSqlEntityProviderConfigurationSection,
		Devart.Data.PostgreSql.Entity" />
</configSections>
<!-- Devart specific configuration (enables creation of database) -->
<Devart.Data.PostgreSql.Entity xmlns="http://devart.com/schemas/Devart.Data.PostgreSql.Entity/1.0">
	<DatabaseScript>
		<Schema DeleteDatabaseBehaviour="Database" />
	</DatabaseScript>
</Devart.Data.PostgreSql.Entity>
Seems to work now as expected, I have one question though. DotConnect creates an empty schema called "Current SCHEMA {GUID}" - whats the purpose of this and can this be avoided?

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

Re: Code-First CreateDatabase supported?

Post by Shalex » Thu 26 Jun 2014 14:48

Please make sure (via dbMonitor) that the "Current SCHEMA {GUID}" schema is created by dotConnect for PostgreSQL. If so, specify the exact (x.x.x) version of your dotConnect for PostgreSQL and send us a small test project so that we can reproduce the issue in our environment.

dresel
Posts: 18
Joined: Tue 20 Nov 2012 13:38

Re: Code-First CreateDatabase supported?

Post by dresel » Fri 27 Jun 2014 10:29

I verified (via dbMonitor) that this statement (CREATE SCHEMA "Current SCHEMA {C353AD3D-7207-416E-A4C4-7FE3BEE0E887}") is executed by dotConnect.

I'm using Devart dotConnect for PostgreSQL Trial 7.3.181.0 (Devart.Data.PostgresSql.Entity shows 7.3.181.6) with EntityFramework 6.1.1 and PostgreSQL 9.3.

I will send you a test project.

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

Re: Code-First CreateDatabase supported?

Post by Shalex » Fri 27 Jun 2014 16:03

Thank you for your test project. We have reproduced the issue and are investigating it.

dresel
Posts: 18
Joined: Tue 20 Nov 2012 13:38

Re: Code-First CreateDatabase supported?

Post by dresel » Mon 07 Jul 2014 07:43

I also found out, that Migrations are not executed when using context.Database.Create, but when you do something like

Code: Select all

context.TestEntities.Create();
which triggers a database creation then Migrations are executed.

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

Re: Code-First CreateDatabase supported?

Post by Shalex » Tue 08 Jul 2014 17:02

dresel wrote:I verified (via dbMonitor) that this statement (CREATE SCHEMA "Current SCHEMA {C353AD3D-7207-416E-A4C4-7FE3BEE0E887}") is executed by dotConnect.
The bug with creating extra schema in the database when config.DatabaseScript.Schema.DeleteDatabaseBehaviour = DeleteDatabaseBehaviour.Database is fixed. We will notify you when the corresponding build of dotConnect for PostgreSQL is available for download.
dresel wrote:I also found out, that Migrations are not executed when using context.Database.Create, but when you do something like

Code: Select all

context.TestEntities.Create();
which triggers a database creation then Migrations are executed.
The test project you have sent us includes the code:

Code: Select all

		private static void Main(string[] args)
		{
			PgSqlMonitor monitor = new PgSqlMonitor() { IsActive = true };
			TestContext context = new TestContext();
			if (context.Database.Exists())
			{
				context.Database.Delete();
			}
			context.Database.Create();
And this code does (re)create a database with the "TestEntity" table. How should we modify your test project to reproduce the issue?

dresel
Posts: 18
Joined: Tue 20 Nov 2012 13:38

Re: Code-First CreateDatabase supported?

Post by dresel » Thu 10 Jul 2014 07:20

Yes. Add

Code: Select all

Console.WriteLine("Migrations executed.");
to InitialCreate.Up().

When you run the code, Up will not be called. When you change

Code: Select all

context.Database.Create();
to

Code: Select all

context.TestEntities.Create();
Migrations will be called.

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

Re: Code-First CreateDatabase supported?

Post by Shalex » Fri 11 Jul 2014 12:44

Shalex wrote:The bug with creating extra schema in the database when config.DatabaseScript.Schema.DeleteDatabaseBehaviour = DeleteDatabaseBehaviour.Database is fixed. We will notify you when the corresponding build of dotConnect for PostgreSQL is available for download.
New build of dotConnect for PostgreSQL 7.3.201 is available for download!
It can be downloaded from http://www.devart.com/dotconnect/postgr ... nload.html (trial version) or from Registered Users' Area (for users with active subscription only).
For more information, please refer to http://forums.devart.com/viewtopic.php?f=3&t=29946.
dresel wrote:Add

Code: Select all

Console.WriteLine("Migrations executed.");
to InitialCreate.Up().

When you run the code, Up will not be called.
We have reproduced the issue and are investigating it.

Post Reply