question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NpgsqlMigrationsSqlGenerator issue after upgrading to EF Core 5.0.1

See original GitHub issue

I’ve just upgraded to 5.0.1 and my custom migration SQL generator failed.

I replace it like below

  .ReplaceService<IMigrationsSqlGenerator, MyMigrationSqlGenerator>()

All tables and constraints are created successfully but, while starting to seeding the cities table for a specific schema, I got the exception;

“There is no entity type mapped to the table ‘sample_tenant.cities’ used in a data operation. Either add the corresponding entity type to the model or specify the column types in the data operation.”

I got an exception on the line below of Generate( ) method for an InsertDataOperation

  base.Generate(operation, model, builder);

Whole MyMigrationSqlGenerator class;


    public class PmsMigrationSqlGenerator : NpgsqlMigrationsSqlGenerator
    {
        private readonly string _schema;

        public PmsMigrationSqlGenerator(MigrationsSqlGeneratorDependencies dependencies, INpgsqlOptions npgsqlOptions)
            : base(dependencies, npgsqlOptions)
        {
            _schema = UserLocalContext.Instance.GetTenantCode();
        }
        protected override void Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
        {
            Action action;
            switch (operation)
            {
                case EnsureSchemaOperation op:
                    action = ProcessEnsureSchemaOperation(op);
                    break;

                case CreateTableOperation op:
                    action = ProcessCreateTableOperation(op);
                    break;

                case SqlOperation op:
                    action = ProcessQueryOperation(op);
                    break;

                default:
                    action = ProcessBaseMigrationOperation(operation);
                    break;
            }

            if (action == Action.Skip)
                return;

            base.Generate(operation, model, builder);
        }

        private Action ProcessQueryOperation(SqlOperation op)
        {
            op.Sql = op.Sql.Replace("#schema", _schema);
            return Action.Apply;
        }

        private Action ProcessEnsureSchemaOperation(EnsureSchemaOperation op)
        {
            op.Name = _schema;
            return Action.Apply;
        }

        private Action ProcessCreateTableOperation(CreateTableOperation op)
        {
            if (ProcessBaseMigrationOperation(op) == Action.Skip)
                return Action.Skip;

            foreach (var item in op.UniqueConstraints)
                item.Schema = _schema;

            foreach (var foreignKey in op.ForeignKeys)
            {
                foreignKey.Schema = _schema;
                foreignKey.PrincipalSchema = _schema;
            }

            return Action.Apply;
        }

        private Action ProcessBaseMigrationOperation(MigrationOperation op)
        {
            var schemaProperty = op.GetType().GetProperties().FirstOrDefault(p => p.Name == "Schema");

            if (schemaProperty != null)
                schemaProperty.SetValue(op, _schema);

            var principleSchemaProperty = op.GetType().GetProperties().FirstOrDefault(p => p.Name == "PrincipalSchema");

            if (principleSchemaProperty == null)
                return Action.Apply;

            principleSchemaProperty.SetValue(op, _schema);

            return Action.Apply;
        }

        private enum Action
        {
            Apply,
            Skip
        }
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
AndriySvyrydcommented, Dec 14, 2020

op.KeyColumnTypes = table.PrimaryKey.Columns.Select(s => s.Name).ToArray();

I think you meant op.KeyColumnTypes = table.PrimaryKey.Columns.Select(s => s.StoreType).ToArray();

2reactions
AndriySvyrydcommented, Dec 12, 2020

@suadev As the exception message states “Either add the corresponding entity type to the model or specify the column types in the data operation.” So if you can’t alter the model appropriately then specify the column types in the InsertDataOperation. Though there’s currently an issue when it doesn’t actually flow through, so be sure to vote for it to be fixed soon: https://github.com/dotnet/efcore/issues/23503

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get an error when I add migration using Entity Framework ...
I just ran into this same issue using VS for Mac . My problem was I had the following versions of packages installed:...
Read more >
Breaking changes in EF Core 5.0
Complete list of breaking changes introduced in Entity Framework Core 5.0.
Read more >
Migration Error Using PostgreSQL on Fresh Project #8554
Hi, after following the instruction from docs on using the PostgreSQL, Im getting following error upon migrating. Im using fresh project of ...
Read more >
Migration from Asp.Net Core 5.0 to 6.0 — Real project
In the rest of the article, I'm going to demonstrate all the steps I made to migrate a real project, all the problems,...
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
In this post, we'll look at some pitfalls and ideas EF Core users like yourself may want to consider when developing an application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found