Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold-DbContext not adding schema parameter to modelBuilder.HasSequence #15513

Closed
y-code opened this issue Apr 28, 2019 · 0 comments · Fixed by #15514
Closed

Scaffold-DbContext not adding schema parameter to modelBuilder.HasSequence #15513

y-code opened this issue Apr 28, 2019 · 0 comments · Fixed by #15514
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@y-code
Copy link
Contributor

y-code commented Apr 28, 2019

DB context scaffold does not generate modelBuilder.HasSequence method call with schema parameter in DbContext's OnModelCreating method.

            modelBuilder.HasSequence<int>("message_id_seq");

For me, it caused a problem in table creation when initializing a PostgreSQL database with a DB context generated by DB context scaffold.

Because my table has a column of serial type, the table depends on a sequence in the same schema. However, the generated DB context does not specify the schema for the sequence because of the scaffold's behavior above, and thus the sequence does not exist in the same schema when the table is created and the table creation fails.

The followings are SQL that created the original table, and the error that I got from dotnet ef dbcontext scaffold.

CREATE TABLE sample.message (
  id serial PRIMARY KEY,
  message text
);
Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE sample.message (
    id integer NOT NULL DEFAULT (nextval('sample.message_id_seq'::regclass)),
    message text NULL,
    CONSTRAINT "PK_message" PRIMARY KEY (id)
);
Disposing transaction.
Closing connection to database 'sampledb' on server 'tcp://localhost:2345'.
Closed connection to database 'sampledb' on server 'tcp://localhost:2345'.
'SampleDbContext' disposed.
Npgsql.PostgresException (0x80004005): 42P01: relation "sample.message_id_seq" does not exist
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 490
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1218
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1042
   at Npgsql.NpgsqlCommand.ExecuteNonQuery() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1025
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

I confirmed that the table creation succeeds once I add the schema parameter to the modelBuilder.HasSequence method call as shown below. Therefore, the schema parameter should be appended in the generated code as long as database provider provides the sequence's schema.

            modelBuilder.HasSequence<int>("message_id_seq", "sample");

I also confirmed it in the PostgreSQL database provider that NpgsqlDatabaseModelFactory does set sequence schema before adding sequence info to DatabaseModel, so that something is wrong in EF Core.

Steps to reproduce

  1. Start an empty PostgreSQL Server in docker
  2. Create a database and a table with a "serial" type column
  3. Run DB Context Scaffold
  4. Start another empty PostgreSQL Server in docker
  5. Add a migration for the generated DB Context
  6. Install the database by the DB Context
  7. Test the installed database table

I uploaded test code to reproduce the issue to one of my repositories.

Further technical details

.NET Core SDK 2.2.105
EF Core version: 2.2.4
Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0
Operating system: macOS

@divega divega added this to the 3.0.0 milestone Apr 29, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 30, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview6 Jun 5, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview6, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants