-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Error in Generated Migration Script for dotnet-ef 8.0.11 #35132
Comments
And the same error happens also when using dotnet-ef version 9.0.0 |
After further testing, I concluded that even if I downgrade the dotnet-ef version the pipeline uses to 7.0.20 (which is known to have worked before), the migration scripts are still missing the batch separator between statements (thus throwing the same error "The variable name '@defaultSchema' has already been declared. Variable names must be unique within a query batch or stored procedure." even if dotnet-ef explicitly uses 7.0.20). The reason for this might be the fact that the dotnet project that contains the migrations is targeting <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0"> Another hint that might point towards this direction is that running the command So, to conclude, our last working configuration was the combination of using dotnet-ef 7.0.20 in the pipeline and targeting Microsoft.EntityFrameworkCore.* 7.0.19 in the project. Once either of these targets a higher version, the migration script is no longer split into independent batches, resulting in the error mentioned above. |
@costinbanu The |
Makes sense. Then I guess this is an issue within Microsoft.EntityFrameworkCore.SqlServer 9.0.0. |
I just ran into this as well and found a possible workaround (even though it isn't perfect). Creating the migration script as usual removes the GO statements as reported above. > dotnet ef migrations script --idempotent --context MyContext --project ./src/MyProject/MyProject.csproj --output ./migration-script.sql BEGIN TRANSACTION;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20210210142332_MigrationReset'
)
BEGIN
CREATE TABLE [Companies] (
[Id] uniqueidentifier NOT NULL,
[Name] nvarchar(max) NOT NULL,
[CreatedAt] datetime2 NOT NULL DEFAULT (getdate()),
[UpdatedAt] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Companies] PRIMARY KEY ([Id])
);
END;
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20210210142332_MigrationReset'
)
BEGIN
CREATE TABLE [Projects] (
[Id] uniqueidentifier NOT NULL,
[ProjectNumber] nvarchar(25) NOT NULL,
[ProjectName] nvarchar(255) NOT NULL,
[OperationsManager] nvarchar(255) NOT NULL,
[CreatedAt] datetime2 NOT NULL DEFAULT (getdate()),
[UpdatedAt] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Projects] PRIMARY KEY ([Id])
);
END;
COMMIT;
GO But, if I pass the > dotnet ef migrations script --idempotent --context MyContext --project ./src/MyProject/MyProject.csproj --output ./migration-script.sql --no-transactions IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20210210142332_MigrationReset'
)
BEGIN
CREATE TABLE [Companies] (
[Id] uniqueidentifier NOT NULL,
[Name] nvarchar(max) NOT NULL,
[CreatedAt] datetime2 NOT NULL DEFAULT (getdate()),
[UpdatedAt] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Companies] PRIMARY KEY ([Id])
);
END;
GO
IF NOT EXISTS (
SELECT * FROM [__EFMigrationsHistory]
WHERE [MigrationId] = N'20210210142332_MigrationReset'
)
BEGIN
CREATE TABLE [Projects] (
[Id] uniqueidentifier NOT NULL,
[ProjectNumber] nvarchar(25) NOT NULL,
[ProjectName] nvarchar(255) NOT NULL,
[OperationsManager] nvarchar(255) NOT NULL,
[CreatedAt] datetime2 NOT NULL DEFAULT (getdate()),
[UpdatedAt] datetime2 NOT NULL DEFAULT (getdate()),
CONSTRAINT [PK_Projects] PRIMARY KEY ([Id])
);
END;
GO |
have this problem as well with temporal tables schema names. |
We have a dotnet project that we upgraded from dotnet 7 to dotnet 8. We also updated dotnet-ef from 7.0.2.0 to 8.0.11 in the pipeline that we use. This change has introduced a bug when generating migration scripts
Generated SQL script with version 8.0.11
This fails because the variable
@defaultSchema
is declared twice in the same SQL batch. Let's have a look at the old version SQL output:Generated SQL script with version 7.0.20
Here you can see that besides the order of the migrations and some formatting, the new version is lacking the sql batch separator
GO
which is, essentially, the source of the problemSteps to reproduce
Expected behavior
The script is executed successfully
Actual behavior
The deployment fails / Azure DevOps pipeline step
SqlAzureDacpacDeployment
fails with message##[error]The variable name '@defaultSchema' has already been declared. Variable names must be unique within a query batch or stored procedure.Check out how to troubleshoot failures at https://aka.ms/sqlazuredeployreadme#troubleshooting-
Provider and version information
EF Core version: 8.0.11
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: private Azure DevOps pipeline agent running on Windows 10 x64
IDE:
The text was updated successfully, but these errors were encountered: