Skip to content

Scaffolding fails because of dropped column traces #917

Closed

Description

@eBerdnA

Right now I'm unable to scaffold a model for an existing postgres database. I do get the error message Column is null.

I set up two demo projects which I used to reproduce this effect even with a completely new project.

ScaffoldTestNET
ScaffoldTestDnx

As you can see one is using dnx and the other is using .NET 4.6.1

The error message is the same for both projects. A dump of the database I have been using for this can be found here: https://gist.github.com/eBerdnA/e5f790dc205e6dc07818

Following the stacktrace when I run dnx ef dbcontext scaffold "Host=localhost;Username=postgres;Password=test1234;Database=Homepage" EntityFramework7.Npgsql --outputDir Models in ScaffoldTestDnx

System.InvalidCastException: Column is null
   at Npgsql.BackendMessages.DataRowMessage.CheckNotNull()
   at Npgsql.NpgsqlDataReader.ReadColumnWithoutCache[T](Int32 ordinal)
   at Npgsql.NpgsqlDataReader.GetString(Int32 ordinal)
   at Microsoft.Data.Entity.Scaffolding.NpgsqlDatabaseModelFactory.GetColumns()
   at Microsoft.Data.Entity.Scaffolding.NpgsqlDatabaseModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.Data.Entity.Scaffolding.RelationalScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.Data.Entity.Scaffolding.NpgsqlScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
   at Microsoft.Data.Entity.Scaffolding.Internal.ReverseEngineeringGenerator.GetMetadataModel(ReverseEngineeringConfiguration configuration)
   at Microsoft.Data.Entity.Scaffolding.Internal.ReverseEngineeringGenerator.GenerateAsync(ReverseEngineeringConfiguration configuration, CancellationToken cancellationToken)
   at Microsoft.Data.Entity.Design.DatabaseOperations.ReverseEngineerAsync(String provider, String connectionString, String outputDir, String dbContextClassName, List`1 schemas, List`1 tables, Boolean useDataAnnotations, CancellationToken cancellationToken)
   at Microsoft.Data.Entity.Commands.Program.Executor.<>c__DisplayClass11_0.<<ReverseEngineerAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Commands.Program.Executor.<ExecuteAsync>d__13.MoveNext()
Column is null

If any further information or help is needed just let me know.

Activity

  1. eBerdnA commented on Dec 27, 2015

    @eBerdnA
    Author

    I'm very sorry for opening this issue. But somehow my local test database seems to be "corrupted". After creating a new local db from my gist I noticed that scaffolding works without any issue.

    Then I created a full backup including data and restored it as a new database. Then I was also able to scaffold.

    Therefore somehow my local db seems to be "corrupted" even though I don't know where exactly.

    Due to the fact that I can't even reproduce this effect on my machine with a full backup of the db I will close this issue.

  2. reopened this on Dec 29, 2015
  3. eBerdnA commented on Dec 29, 2015

    @eBerdnA
    Author

    I think I found a way to reproduce this effect. The following steps should lead to the problem.

    1 - First of all create two tables.

    CREATE TABLE "Article"
    (
      "ArticleId" serial NOT NULL,
      "Title" character varying,
      CONSTRAINT "Article_pkey" PRIMARY KEY ("ArticleId")
    )
    WITH (
      OIDS=FALSE
    );
    ALTER TABLE "Article"
      OWNER TO postgres;
    
    CREATE TABLE "Status"
    (
      "StatusId" serial NOT NULL,
      "Title" character varying(255),
      CONSTRAINT "Status_pkey" PRIMARY KEY ("StatusId")
    )
    WITH (
      OIDS=FALSE
    );
    ALTER TABLE "Status"
      OWNER TO postgres;
    

    Scaffolding should be possible now.

    2 - Add a new column to table Article and a constraint

    ALTER TABLE "Article"
       ADD COLUMN "StatusId" integer;
    
    ALTER TABLE "Article"
      ADD CONSTRAINT "fkey_Status" FOREIGN KEY ("StatusId") REFERENCES "Status" ("StatusId")
       ON UPDATE NO ACTION ON DELETE NO ACTION;
    

    Scaffolding should still be possible.

    3 - Remove the added constraint and the column from table Article.

    Now scaffolding should fail with Column is null.

  4. roji commented on Dec 29, 2015

    @roji
    Member

    This happens here, will investigate.

  5. changed the title [-]Can't scaffold database[/-] [+]Scaffolding fails because of dropped column traces[/+] on Dec 29, 2015
  6. roji commented on Dec 29, 2015

    @roji
    Member

    @eBerdnA fixed this, thanks for the time spent isolating it!

    PostgreSQL's pg_attribute table, which the scaffolding process consults, contains traces of dropped columns which were causing errors. This is why the error was only reproducible after dropping columns.

    I am in the process of making the work again on our build server, at which point unstable nugets with this fix will automatically appear in our unstable myget feed. This should happen in the next few days.

    Thanks again, and continue reporting any other issues you find!

  7. self-assigned this
    on Dec 29, 2015
  8. added a commit that references this issue on Jan 11, 2016
    d675927
  9. adopilot commented on May 26, 2017

    @adopilot

    @roji Unfortunately Looks like we again have issues with Scaffolding DBs which changed structure.
    In VS2016 CE whit packages

      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
        <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" />
        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
    

    When I try to

    Scaffold-DbContext "Host=forum.robot.ba;Database=XXXX;Username=XXX;Password=XXX" Npgsql.EntityFrameworkCore.PostgreSQL -o Data\Forum -Context ForumDbContext

    I get exception

    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
       at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.NpgsqlDatabaseModelFactory.GetIndexes()
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.NpgsqlDatabaseModelFactory.Create(DbConnection connection, TableSelectionSet tableSelectionSet)
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.NpgsqlDatabaseModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
       at Microsoft.EntityFrameworkCore.Scaffolding.RelationalScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.NpgsqlScaffoldingModelFactory.Create(String connectionString, TableSelectionSet tableSelectionSet)
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GetMetadataModel(ReverseEngineeringConfiguration configuration)
       at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineeringGenerator.GenerateAsync(ReverseEngineeringConfiguration configuration, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContextAsync(String provider, String connectionString, String outputDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<ScaffoldContextImpl>d__22.MoveNext()
       at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
       at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
       at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass4_0`1.<Execute>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
    Index was out of range. Must be non-negative and less than the size of the collection.
    

    When I do dump Only schema and restore it to other new database Scaffolding whent only whit warnings.

    Ignoring unsupported index unique_index_categories_on_name which contains an expression (COALESCE(parent_category_id, '-1'::integer))
    Ignoring unsupported index index_topics_on_lower_title which contains an expression (lower((title)::text))
    Ignoring unsupported index index_post_custom_fields_on_name_and_value which contains an expression ("left"(value, 200))
    Unable to identify the primary key for table 'public.post_replies'.
    Unable to generate entity type for table 'public.post_replies'.
    Unable to identify the primary key for table 'public.post_timings'.
    Unable to generate entity type for table 'public.post_timings'.
    Unable to identify the primary key for table 'public.schema_migrations'.
    Unable to generate entity type for table 'public.schema_migrations'.
    Unable to identify the primary key for table 'public.topic_views'.
    Unable to generate entity type for table 'public.topic_views'.
    Ignoring unsupported index index_users_on_email which contains an expression (lower((email)::text))
    

    But I do get context and looks to me good code whic compile and run.

    I rely do not what changes occurs on my original pg database because I do not own it and Do not deply it.

  10. roji commented on May 26, 2017

    @roji
    Member

    @adopilot this issue has been fixed, but version 1.1.1 hasn't been released yet. I hope this will happen in the next week or two, at which point the problem should go away.

  11. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions