-
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
EFCore 1.1 doesn't use an existing property for the FK if it's part of the primary key #7127
Comments
@gius Could you provide a complete model and context? I am not able to repro the issue with just the above code. |
@gius Nevermind, I found the issue. It is still by design that EF doesn't use
should be enough, independently of the order. |
… the name matches even if the principal end hasn't been configured. Fixes #7127
Fixed in 16eb5b3 |
This patch bug is approved. Please use the normal code review process w/ a PR and make sure the fix is in the correct branch, then close the bug and mark it as done. |
✅ Verified |
The problem occurs in much complicated entities setup but I have isolated the problem to the following.
Steps to reproduce
Have entity whose primary key is also a reference:
The issue
Versions prior to EFCore version 1.1 created both, primary key as well as foreign key, all worked properly. After updating to version 1.1, SQL generated by EF is wrong - it expects a new column OrderID1.
Obviously the model is wrong - when adding a new migration (before the EF update, this migration would be empty), the migration tries to drop foreign key "FK_OrderDetail_Order_OrderID", drop index "IX_OrderDetail_OrderID", alter column "OrderID" to make it "SqlServerValueGenerationStrategy.IdentityColumn", add new column "OrderID1" (nullable), and create index and foreign key for the new column "OrderID1".
Note that the reference from OrderDetail to Order tables is not explicitly described, can be only inferred from OrderID and Order properties.
Is this expected?
Further technical details
EF Core version: 1.1.0
Operating system: Windows 10
Visual Studio version: VS 2015
Update 1
I've tried to explicitly set the reference as follows:
And it did not help. However, changing the order of the lines seems to have solved the problem:
Update 2
It also seems that many of implicit indexes on foreign keys have been removed but explicit call to
.HasIndex
fixes this. Especially indexes on primary keys that are also foreign keys and indexes on the first column of composed primary keys.The text was updated successfully, but these errors were encountered: