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.

IMigrator.GenerateScript() creates invalid Access SQL for the __EFMigrationsHistory table check

See original GitHub issue

When creating a migration sql script for a context with migrations, the scripts contains an invalid statement:

IF NOT EXISTS (SELECT * FROM `INFORMATION_SCHEMA.TABLES` WHERE `TABLE_NAME` = '__EFMigrationsHistory') THEN CREATE TABLE `__EFMigrationsHistory` (
        `MigrationId` varchar(150) NOT NULL,
        `ProductVersion` varchar(32) NOT NULL,
        CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
    );
;

This results in “Invalid SQL statement; expected ‘DELETE’, ‘INSERT’, ‘PROCEDURE’, ‘SELECT’, or ‘UPDATE’.” ACCESS SQL doesn’t support the IF statement.

            await context.Database.EnsureDeletedAsync();
            await context.Database.EnsureCreatedAsync();
            var pendingMigrations = (await context.Database.GetPendingMigrationsAsync()).ToList();
            
            var migrator = context.Database.GetService<IMigrator>();
            string last = null;
            string current = null;
            try
            {
                //await context.Database.EnsureCreatedAsync();
                foreach (var migration in pendingMigrations)
                {
                    current = migration;
                    var upSql = migrator.GenerateScript(last, current,
                        idempotent: false);
                    using (var transaction = await context.Database.BeginTransactionAsync())

                    {
                        await context.Database.ExecuteSqlRawAsync(upSql);
                    }
                }
            }
            catch (Exception e)
            {
                throw new AggregateException($"Up Migration {current} failed.", e);
            }

Applying the Migration directly with MigrateAsync() works correctly.

Issue114.zip

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MichaelSteineckecommented, Jan 24, 2022

That works! Thank you 😃

0reactions
lauxjpncommented, Jan 21, 2022

Is it possible to use the EntityFrameworkCore.Jet.Data based procedural statements when creating migrations or execute SQL?

@MichaelSteinecke As long as you run the statements through our EntityFrameworkCore.Jet.Data ADO.NET provider (which is implicitly being used by EntityFrameworkCore.Jet), and as long as those statements are correctly formatted so that the provider recognizes them, this should work fine, since it is exactly what our internal query generator and migration script generator does.

For example, most procedural statement support was added to EntityFrameworkCore.Jet.Data because it was necessary to make migrations work.


For example I need to create a view which may or may not exists before.

This should work.

The general IF (...) THEN ... statement syntax can be seen here:

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/JetCommand.cs#L28

The general INFORMATION_SCHEMA support can be seen here:

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/JetStoreSchemaDefinition/JetInformationSchema.cs#L14-L16

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/JetStoreSchemaDefinition/JetInformationSchema.cs#L24-L33

You would check for views in a similar way as for tables, by using INFORMATION_SCHEMA.TABLES. The default SchemaProvider that is being used to retrieve this information is PreciseSchema, which will just forward the GetTables() call to AdoxSchema:

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/PreciseSchema.cs#L20-L21

AdoxSchema.GetTables() will then return both, tables and views:

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/AdoxSchema.cs#L78-L157

The format of the returned table can be seen here:

https://github.com/bubibubi/EntityFrameworkCore.Jet/blob/7de5da5755e1146f1cf51e260227bea3da8d7784/src/EFCore.Jet.Data/JetStoreSchemaDefinition/SchemaTables.cs#L7-L21

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Core - get script of pending migrations
1 Answer 1 ... Here is one approach to dumping the Entity Framework Core SQL scripts for troubleshooting, or adding them to source...
Read more >
SQL Server Migrations: Idempotent scripts fails with 'invalid ...
It could be solved by using dynamic a sql script, as: IF NOT EXISTS(SELECT * FROM [__EFMigrationsHistory] WHERE [MigrationId] = N' ...
Read more >
IMigrator.GenerateScript Method
Generates a SQL script to migrate a database either in its entirety, or starting and ending at specified migrations.
Read more >
What's New in EF Core 5 - Learn Entity Framework Core 7
EF Core 5.0 migrations can now generate CHECK constraints for enum ... Entity<User>() ... On SQL Server, the migration will create the following...
Read more >
EF Core Migrations
EF Core Migrations automatically generates and executes the necessary SQL scripts to update the database schema, so you don't have to write ...
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