Repository navigation
Description
Hi,
I tried to migrate my solution to .NET 9, incl. EF Core 9 with SQL Server as the provider.
Suddenly one of my migrations (old one from .NET 7 times) fails. I looked into the differences between the migration code and it appears that the transaction for migrations is now one large transaction per DbContext? and not one migration per .cs migration file.
If I modify my migration to use suppressTransaction: true from https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli it works again.
Is that an intended breaking change or some config parameter I missed in the migration?
Log see below:
.NET 8:
2024-11-13 15:52:31.8230 - Info - ThreadId: 16 - TraceId: na - Identity: System - Migrations: Applying migration '20230313093754_RemoveNodesMetadataId'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Connection: Opening connection to database 'AnnotationsTestsDb' on server 'localhost'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Connection: Opened connection to database 'AnnotationsTestsDb' on server 'localhost'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Transaction: Beginning transaction with isolation level 'Unspecified'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Transaction: Began transaction with isolation level 'ReadCommitted'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Command: Creating DbCommand for 'ExecuteNonQuery'.
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Command: Created DbCommand for 'ExecuteNonQuery' (0ms).
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Command: Initialized DbCommand for 'ExecuteNonQuery' (0ms).
2024-11-13 15:52:31.8230 - Debug - ThreadId: 16 - TraceId: na - Identity: System - Command: Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @ToDeletePredecessor AS TVP_BIGINT;
INSERT INTO @ToDeletePredecessor SELECT DISTINCT e.Id FROM isPredecessorOf e LEFT OUTER JOIN Nodes n1 ON n1.$node_id = e.$from_id LEFT OUTER JOIN Nodes n2 ON n2.$node_id = e.$to_id WHERE n1.Id IS NULL OR n2.Id IS NULL;
DELETE FROM GraphPaths WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);
DELETE FROM isPredecessorOf WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);
2024-11-13 15:52:31.8230 - Info - ThreadId: 19 - TraceId: na - Identity: System - Command: Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @ToDeletePredecessor AS TVP_BIGINT;
INSERT INTO @ToDeletePredecessor SELECT DISTINCT e.Id FROM isPredecessorOf e LEFT OUTER JOIN Nodes n1 ON n1.$node_id = e.$from_id LEFT OUTER JOIN Nodes n2 ON n2.$node_id = e.$to_id WHERE n1.Id IS NULL OR n2.Id IS NULL;
DELETE FROM GraphPaths WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);
DELETE FROM isPredecessorOf WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);.NET9:
2024-11-13 16:05:04.0882 - Info - ThreadId: 15 - TraceId: na - Identity: System - Migrations: Applying migration '20230313093754_RemoveNodesMetadataId'.
2024-11-13 16:05:04.0882 - Debug - ThreadId: 15 - TraceId: na - Identity: System - Command: Creating DbCommand for 'ExecuteNonQuery'.
2024-11-13 16:05:04.0882 - Debug - ThreadId: 15 - TraceId: na - Identity: System - Command: Created DbCommand for 'ExecuteNonQuery' (0ms).
2024-11-13 16:05:04.0882 - Debug - ThreadId: 15 - TraceId: na - Identity: System - Command: Initialized DbCommand for 'ExecuteNonQuery' (0ms).
2024-11-13 16:05:04.0882 - Debug - ThreadId: 15 - TraceId: na - Identity: System - Command: Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @ToDeletePredecessor AS TVP_BIGINT;
INSERT INTO @ToDeletePredecessor SELECT DISTINCT e.Id FROM isPredecessorOf e LEFT OUTER JOIN Nodes n1 ON n1.$node_id = e.$from_id LEFT OUTER JOIN Nodes n2 ON n2.$node_id = e.$to_id WHERE n1.Id IS NULL OR n2.Id IS NULL;
DELETE FROM GraphPaths WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);
DELETE FROM isPredecessorOf WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor)
2024-11-13 16:05:05.1854 - Error - ThreadId: 15 - TraceId: na - Identity: System - Command: Failed executing DbCommand (1,091ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @ToDeletePredecessor AS TVP_BIGINT;
INSERT INTO @ToDeletePredecessor SELECT DISTINCT e.Id FROM isPredecessorOf e LEFT OUTER JOIN Nodes n1 ON n1.$node_id = e.$from_id LEFT OUTER JOIN Nodes n2 ON n2.$node_id = e.$to_id WHERE n1.Id IS NULL OR n2.Id IS NULL;
DELETE FROM GraphPaths WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor);
DELETE FROM isPredecessorOf WHERE Id IN (SELECT [Value] FROM @ToDeletePredecessor)
2024-11-13 16:05:05.1854 - Debug - ThreadId: 15 - TraceId: na - Identity: System - Transaction: Disposing transaction.(I don't really know why this specific sql snippet fails in the existing migration and I have not really looked into that in any detail, I guess it might be related to the TVP not being available in that transaction, but that's just a wild guess).
Activity
roji commentedon Nov 13, 2024
@Tornhoof can you please share the part of the migration which causes the error when executing in a transaction?
Tornhoof commentedon Nov 13, 2024
I'm not sure how much you need, what specifically fails are the 4 sql statements in the log file, the first first one in that file fails.
This is directly the first call to
migrationBuilder.Sqlin this specific migration source file. If I comment it or use the suppressTransaction arg everything works. It's not the first migration file, nor the last, nor is it the first or last using sql statements or even the aforementioned TVP_BigInt.The full migration file, which includes these sql statements is below in details. The migration is basically three different sql scripts, first one cleans some SQL graph tables up, then removes a column in a SQL Graph tables and changes a few constrains and the third one recreates an unrelated table-valued-parameter. The migrations are just used to run the scripts, there are no EF core model changes involved.
Details
roji commentedon Nov 13, 2024
How does it fail, what's the error? We need some details in order to investigate this.
Tornhoof commentedon Nov 13, 2024
It happens during the unit tests which create a db via
graphContext.Database.MigrateAsyncon an non-existing db.It also happens if I run the affected migration against an existing db which has old enough state.
The exception is the following:
If I read the What's new Document correctly, EF Core 9 now tries to block concurrent migrations, I don't know if Resource deadlock is the expected error if that happens, but I'm really sure that there are no concurrent migrations here and commenting out a specific sql statement should have no impact on concurrent migrations anyway, so I did put that into the topic.
roji commentedon Nov 13, 2024
That's very odd indeed. Are you able to put together a minimal repro that shows this happening? That would be the best way to help us investigate and fix this.
/cc @AndriySvyryd
Tornhoof commentedon Nov 13, 2024
I'll see what I can cobble together
Tornhoof commentedon Nov 14, 2024
I wrote a repro here: https://github.com/Tornhoof/MigrationRepro, just clone it and run it (see program.cs for connection string).
It contains two migrations, one creates a tvp, the other one uses it.
As expected above it is the TVP.
Apparently I was wrong and it's the first use of any TVP in any migration, every other migration just creates them.
roji commentedon Nov 14, 2024
Confirmed, see below for a minimal repro without EF: trying to create a TVP and to declare a variable with that TVP in a transaction fails on SQL Server because of a deadlock.
Note that this was a problem in previous versions of EF; the following migration triggers a deadlock also with EF 8.0:
The difference in 9.0 is that the transaction extends across migrations, so the deadlock occurs even when the creation and the declaration are spread across different migrations.
I don't think there's anything we can do here really - the workaround for now would be to suppress the transaction as @Tornhoof said above. We can also reach out to the SQL Server people and discuss this.
Minimal repro without EF
[-]EF Core 9 Migration Transaction change?[/-][+]EF9: single transaction spanning all migrations causes transaction-incompatible SQL to fail[/+]roji commentedon Nov 16, 2024
Thanks for bringing to our attention @Tornhoof. I unfortunately don't think there's anything actionable for us here, plus the problem existed before as well, if you happened to perform both actions in a single migration...
In addition to the workaround of suppressing the transaction, you can also simply apply the two migrations separately, by explicitly specifying on the command-line which migration state you want to migrate to (although you'd have to remember to do that every time).
Other than that, I'll send a note to the SQL Server people about this so that they're aware - you can also follow up with them yourself.
35 remaining items
AndriySvyryd commentedon Jun 6, 2025
@marekott This change bears more risk than what we are willing to take on for servicing. So, we recommend that you wait for EF Core 10.
KrzysztofBranicki commentedon Jun 9, 2025
@AndriySvyryd @roji how about introducing a setting which would allow developers to control this behavior? This has no risk for servicing release, default value can be consistent with current behavior in .NET9 but at least we have ability to change it. With this approach of changing it back and forth without giving developer ability to adjust the behavior it is like you are committed to introducing breaking change every year EF is released.
roji commentedon Jun 9, 2025
@KrzysztofBranicki introducing a setting makes sense when there are actual, legitimate reasons why users would sometimes need one setting, and sometimes another; this doesn't seem to be one of those cases. Settings should be added with care: they increase complexity as users need to learn about them, understand what they mean, etc.
I'm also not quite sure why the change being proposed for 10 constitutes a breaking change, as you suggest.
So while I understand the frustration around the EF9 change, this is a case where we unfortunately made a bad decision, and now plan to fix it for 10. This definitely isn't an ideal situation, but I don't see anything warranting a user setting.
KrzysztofBranicki commentedon Jun 10, 2025
@roji Legitimate reason is to enable developers to upgrade their solutions to 9. Setting can be obsolete right from the start and can go away in 10. We are currently upgraded "half way", we upgraded SDK/BCL, ASP to 9 but we have EF, Npgsql and all related plugins in 8. In case of bugs or security issues are you planning to do releases for Npgsql 8.x or you will be only patching 9.x? In other words will Npgsql 8 be supported for the whole lifespan of .NET8 (which is LTS). I'm asking in context of @AndriySvyryd recommendation to stay on 8 and wait for 10.
Regarding the breaking change, it is a significant change in behavior that someone who started his journey of using EF from version 9 can take dependency on. Someone can take a dependency on the fact that if he has multiple migrations in one app release in case one of them fails, his DB schema will not be migrated "half way". Whether that's a good approach to take or not is completely different story but that doesn't change the fact that this can be potentially a breaking change to someone.
roji commentedon Jun 10, 2025
The versioning and support policies are pretty clear - Npgsql generally aligns with .NET support policies and always has, and Npgsql 8 will indeed continue to be supported. So if this is blocking for you, you can indeed stay on 8. Remember also that 10 is planned to be out in a few months at this point, at which point you'll have the option to simply upgrade from 8 to 10.
I'm not sure how someone can "take a dependency" on this. As I said above, it's certainly a behavioral change (and a non-trivial one), but I'm not sure how it can be considered a breaking change. In any case, 9.0 has already been out for many months with the current behavior, and it matters little from this perspective whether someone needs to wait a few more months for 10 to get the behavior change.
Revert migrator behavior to use a transaction for each migration (whe…
Revert migrator behavior to use a transaction for each migration (whe…
Revert migrator behavior to use a transaction for each migration (whe…
Revert migrator behavior to use a transaction for each migration (whe…