-
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
Multiple Foreign Keys and Indices added for inherited items #12963
Comments
This is a duplicate of #12702, but leaving open because it seems we could create only one constraint for this scenario, since the constraints are identical. @razzemans The way to stop two constraints being created is to explicitly specify the constraint name to use: modelBuilder.Entity<HomePage>()
.HasOne(e => e.ContentBlock)
.WithMany()
.HasConstraintName("FK_Pages_ContentBlocks_ContentBlockId");
modelBuilder.Entity<ContentPage>()
.HasOne(e => e.ContentBlock)
.WithMany()
.HasConstraintName("FK_Pages_ContentBlocks_ContentBlockId"); |
That indeed works. Do I understand correctly that this is something you consider fixing in EF itself in a future release? For now, we can continue with the above solution, thank you. |
@razzemans Are you seeing a functional issue with creating two constraints? As in, does it cause an exception/crash/incorrect behavior somewhere? |
@ajcvickers To be fair I haven't tested it, but I figured mutiple indices would definitely have some performance impacts for non-read operations? If that is not the case I'd gladly hear it! If it is relatively easy to fix in EF Core I'd be happy if it gets implemented. It's not a showstopper as there are ways around it. |
@ajcvickers It does crash in my use case. I have a hierarchy with lots of children with many foreign keys each and for each FK EF Core adds an index. It creates more than 999 indexes, causing sql server exception. Would appreciate a fix for this. |
Removing from backlog to consider the index limitation. @GeorgePetri As a workaround, you should be able to delete from the migration all the lines that create the duplicate indexes. |
@AndriySvyryd Assigned to you initially as a model change, which seems ideal. However, it could just be consolidation in Migrations if necessary. |
Related to #10446 |
Closing in favor of #10446 |
Closed the wrong one. |
Verified fixed by afe9774 |
Hello, is there any plan to have a version compatible with .net standard 2.0 including this fix? |
@cyvocross No. The last EF Core version to support .NET Standard is EF Core 3.1. It will be completely out of support in December. |
@ajcvickers ".NET Standard 2.0" ? |
@ErikEJ Good point. I tend to forget about .NET Standard 2.1, since it's largely useless. :-) |
Thank you for your answer, unfortuntly my project run on .Net framework 4.8 and cannot be migrated before before long time due to specific dependecies. |
@cyvocross What are those "specific dependencies" ?? |
Dear @ErikEJ my project depend by internal libs that cannot be migrated on dot net core, therefore i cannot migrate before a long time to dot net 6. |
I'm running into an issue where EF Core generates incorrect migrations. Consider the following code:
This creates the following migration:
This feels incorrect to me. It has the same foreign key and index added twice. Worse, if I have more items that inherit from
BasePage
(which I have in a real world example) I get the same number of indices and foreign keys added as the number of inherited pages (in my case 6).I also tried explicitly adding the following line to the
ContentBlock
entity:public List<BasePage> Pages { get; set; }
But funny enough this only adds another FK and index. I tried setting the FK explicitly using the Fluent API, but it seemed I failed, as it is expecting for example a
List<HomePage>
instead of aList<BasePage>
in theWithMany
method.Am I missing something or is this a bug? I think that this issue appeared after upgrading to EF Core 2.1.1 but I am not 100% sure. I'm quite sure it worked correctly in 1.0 at least.
Further technical details
EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.4
The text was updated successfully, but these errors were encountered: