-
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
Cannot create a unique index / alternate key - and wrong error message is given #28695
Comments
I suspect it would be better not to use implicit keys to discover your issue. In your code Parent seems treated like a field, maybe EF handles this but is not clear I am sure that if you introduce ParentId property as a Guid your troubles will gone, because with explicit keys I have never had such problems. |
My real app explicitly states Id as the primary key and it happens in that too. |
So what about '.HasColumnType("uniqueidentifier")' ? (EF chooses type from your primary key) |
Sorry, I don't know where you are suggesting I put that, and I don't understand how specifying a column type will stop it from being used twice. |
I added ParentId. It should be the same name already present in your db, but hidden in the code. Sometimes previuos migrations are a mess, so it is better to start with a new single one. |
Ah I see. But my property is called I don't use the |
|
I also don't use both X and XId. I use one or the other depending on whether I am within an aggregate or not. I should be able to create a unique index, surely? |
You don't use them, but they exists even if EF hides the XIds. So if you want the parents have only one child a index is necessary. Can exist also parents with the same name? I suppose not, so an unique index for name is also mandatory. You should read https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#one-to-one It seems to me you want a one-to-one and not one-to-many because the index realizes that. And EF can create the index under the cover as you can read. |
I actually have First 1<--->* LinkTable *---->1 Second and just want to ensure I don't have duplicates in the link table. I just checked and you are right about LinkTable having an XId column. I need this to just be called |
@GioviQ I've standardised on XId instead and it works, thank you! I think the error message needs to be more helpful, though. |
@mrpmorris The error message, "'Parent' cannot be used as a property on entity type 'Child' because it is configured as a navigation." is intended to say that in one part of your code the CLR property |
@ajcvickers Yes, that is definitely the confusion. I spent hours trying to understand why my navigation property was invalid. It was only when I stripped down the app to provide a repro that it became apparent it was the index that was the problem. "Parent cannot be used as a property on entity type Child" makes me look at the Child.Parent property class for the problem. To be honest, even knowing what the problem is, I struggle to understand the error message.
This (if I understand correctly) is better because
Please feel free to ask if ever you need to know how simple minded people can possibly misunderstand words :) |
@mrpmorris So we can understand better how you got here, can you provide some details on how you came by writing this? x.HasIndex(x => new { x.Parent, x.Name }).IsUnique(); For example, did you look at the API documentation? Or search for examples and follow them? Or read about indexes in the EF docs? Find it in a book? Etc. (We've been discussing lately how to better help people find the information they need, and part of that is understanding how people attempt to find information, and whether or not they are successful in doing so.) |
@ajcvickers I don't know if I've read that format in the past, or just intuitively guessed, but I just wrote it out on auto-pilot and didn't look into it because it works. But I wrote out the entity class and the whole |
@ajcvickers In answer to your question regarding where I saw it. I was just looking this up again and found this https://learn.microsoft.com/en-us/ef/core/modeling/indexes?tabs=data-annotations
modelBuilder.Entity<Blog>()
.HasIndex(b => new { b.FirstName, b.LastName })
.HasDatabaseName("IX_Names_Ascending");
modelBuilder.Entity<Blog>()
.HasIndex(b => new { b.FirstName, b.LastName })
.HasDatabaseName("IX_Names_Descending")
.IsDescending(); |
I am trying to avoid duplicate entries in a link table.
When I use either
HasIndex
orHasAlternateKey
using a navigation property, not only doesEF migrations
disallow it, but it gives me a wrong error message (which has led me on a wild goose chase for most of the day).Source code
Domain classes
ApplicationDbContext
csproj file
The error message I see is
This has led me to believe my problem is the C# property
Parent
on theChild
class - but it is nothing to do with that. It is theParent
shadow property that is being added by the index.So my ticket has two parts
1: Could the message be changed to something like "Failed to add shadow property 'Parent' on entity type 'Child' when creating an index, because a shadow property named
Parent
has already been defined as a navigation property"2: How do I ensure uniqueness in my link table by combining the Parent and OtherTable columns into a unique index?
Notes
Please note that I considered whether or not this issue is a duplicate of #11336 or not, but decided it wasn't because of the following reasons.
A: This ticket asks for the error message to be changed to make it clear the problem is not with the Child.Parent property the application source code, but a shadow-property issue, and that the message should say it is the index/alternate key causing the problem (showing the index name would be fantastic)
B: I tried the solution proposed in that ticket and it did not solve the issue in this case. See the commented out lines in
ApplicationDbContext
above.The text was updated successfully, but these errors were encountered: