IMigrator.GenerateScript() creates invalid Access SQL for the __EFMigrationsHistory table check
See original GitHub issueWhen 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.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That works! Thank you 😃
@MichaelSteinecke As long as you run the statements through our
EntityFrameworkCore.Jet.Data
ADO.NET provider (which is implicitly being used byEntityFrameworkCore.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.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 defaultSchemaProvider
that is being used to retrieve this information is PreciseSchema, which will just forward theGetTables()
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