efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

EF Core failing when executing migration, containing datetime. Works great with EnsureCreated

Open Babalev opened this issue 8 years ago • 6 comments

I couldn't find a similar issue, sorry if there is already an answer of that.

So I'm having the following problem:

I'm trying to create a database from scratch using the code first approach and EF Core migrations.

When I'm using: context.Database.EnsureCreated(); it works flawlessly and my database gets created.

The problem is when I try with context.Database.Migrate(); Then I hit an error:

Npgsql.PostgresException: '42704: type "datetime" does not exist'

I have some DateTime (C# properties) columns in my entities, but this should not be a problem.

So I'm wondering - why would it work with the first approach, and not with the second one, and is there a solution for this problem?

All kind of help, or guidance appreciated! Thanks!

PS: When I use EnsureCreated() the columns in PostgreSQL db are timestamps (properly translated).

PSS: Here is a SO issue for that.

Babalev avatar Aug 03 '18 16:08 Babalev

@Babalev It sounds like datetime (the literal string) was generated as part of the migration.

But without a minimal, complete reproduction of the issue, this is all just conjecture... Would you be able to put something together for us to test on our end?

austindrenski avatar Aug 03 '18 16:08 austindrenski

Sure. I'll wrap up some very minimalist console

Babalev avatar Aug 03 '18 16:08 Babalev

So after digging in, I found out that the reason for this to happen, is that my Migrations were created against a SQL Server DB, but then I'm trying to execute them against the PostgreSQL one. But why EnsureCreated() works. Isn't it using the same snapshot?

Babalev avatar Aug 03 '18 17:08 Babalev

There's an important distinction between those two, actually.

Per the EF Core docs for RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade):

Applies any pending migrations for the context to the database. Will create the database if it does not already exist.

Note that this API is mutually exclusive with DbContext.Database.EnsureCreated(). EnsureCreated does not use migrations to create the database and therefore the database that is created cannot be later updated using migrations.

austindrenski avatar Aug 03 '18 18:08 austindrenski

I figured out my problem. It was nothing to do with your library, but what you said

It sounds like datetime (the literal string) was generated as part of the migration.

Really helped me to find out the problem. As the Initial migration was scaffold-ed from an existing DB, so that it can continue execute Migrations on top of it, there was a datetime literal there, created by it. Had to remove it, and some more SQL server related strings and now it works like charm against both of the databases - SQL Server and PostgreSQL

Babalev avatar Aug 03 '18 18:08 Babalev

@Babalev Great to hear! I'll go ahead and close this issue.

austindrenski avatar Aug 03 '18 18:08 austindrenski