-
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
SqlServer Migrations: Rebuild foreign keys #12586
Comments
@Anderman Can you provide a project so I dig into this? The index should be dropped before the |
I'm not able to reproduce this on version 2.1.11. The index is correctly dropped and re-created: DROP INDEX [IX_Invoices_ExternalInvoiceId] ON [Invoices];
ALTER TABLE [Invoices] ALTER COLUMN [ExternalInvoiceId] varchar(12) NULL;
CREATE UNIQUE INDEX [IX_Invoices_ExternalInvoiceId] ON [Invoices] ([ExternalInvoiceId]) WHERE [ExternalInvoiceId] IS NOT NULL; |
hi @bricelam I can provide a project to reproduce the problem. https://github.com/maliming/EFCore_12586/commits/master The first step is to create The second step is to create If we update the MaxLength of the Name property of the
|
Just a note on this, I've encountered it on 3.1 when trying to change a PK with a FK reference from nvarchar(450) to nvarchar(250). When generating the script for the migration with 'dotnet ef migrations script', It generates this code to try to remove the PK:
Digging into the [sys].[default_constraints] and [sys].[columns] tables, there's no info about the primary key constraint in there, so this command fails to execute this: Maybe ef needs to generate an If/Else, and in the else it will attempt the normal way of removing a constraint like:
|
FWIW, this is an issue for my team too. |
Also experiencing issues with this when I am trying to alter maxlength of a primary key string column that is used as foreign keys in other tables. This is in .NET 5 |
What's a viable workaround for when running into this issue? Any way to add manual SQL commands or create a multi-step migration to get around the issue? It would be really helpful if you could include a script with an example script that does this. @daunish would you be able to share the script for your solution? (even showing just the lines for one of the dependent tables etc.) |
I have similar problem in my project. I only want to change ApplicationUser primary key from string to int and also for IdentityRole as a result I have to change context to ApplicationDbContext(DbContextOptions options) : IdentityDbContext<ApplicationUser,ApplicationRole,int>(options) but when I execute migration I expect migration should have contained a drop index constraint for those which use this primary key. But it only contain a alter code for Id type which leads to a migration Update-Database error. |
Just stumbled across this while looking for a solution/workaround to a similar problem that I'm having. In my case, I'm initiating a multi-phase process to convert a bunch of existing tables whose PKs are strings (aka varchars), but they're really GUIDs and thus could be more efficiently stored as uniqueidentifiers. Similar to what people have done above by changing the The generated migration correctly identifies what I intend to do (change the data type), but when I run or script the migration the resulting SQL only contains an ALTER COLUMN statement which eventually fails the same as other people above:
I'm currently using EF Core 7. Noteworthy is that this column has no FKs referencing it. However, the PK index on the column itself is what is referencing it. I have other tables I'll need to convert as well that are referenced by other FKs, but I thought I'd start with something simple—a table with no FKs. So now I'm contemplating whether I should try to figure out a way to generate the necessary SQL (maybe in a |
I am also having the exact same issue as @wqoq here but with EF8. Has anybody found a solution to this? EF should really support this scenario... |
I'll follow-up that I did eventually go with a custom SQL script, but not because of the problem I mentioned above—EF not rebuilding the FKs. Ultimately it was because in my particular scenario, due to some poor choices early on, the GUID values were needlessly being stored in a variable-width data type (varchar) and I'm trying to convert them to a fixed-width data type (uniqueidentifier). When done as an ALTER COLUMN, this is incredibly slow. Copying the contents to a new table with the desired data types is much faster. So to get the speed benefits, I had to abandon EF's generated SQL anyway. |
I changed the length of een unique index column with give me the message.
The index 'IX_Invoices_ExternalInvoiceId' is dependent on column 'ExternalInvoiceId'. The index 'IX_Invoices_ExternalInvoiceId' is dependent on column 'ExternalInvoiceId'. ALTER TABLE ALTER COLUMN ExternalInvoiceId failed because one or more objects access this column.
use version
config
old config
generated migration
The text was updated successfully, but these errors were encountered: