Skip to content
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

HasIndex does not work as expected #10135

Closed
fairking opened this issue Oct 21, 2017 · 3 comments
Closed

HasIndex does not work as expected #10135

fairking opened this issue Oct 21, 2017 · 3 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@fairking
Copy link

I'm trying to map something like that:

            b.HasOne(x => x.AppUser)
                .WithMany()
                .IsRequired();

            b.HasIndex(x => x.AppUser)
                .HasName("IX_eo_MyTable_AppUser");

MyTable class:

public class MyTable() {
...
public virtual User AppUser { get; set; }
...
}

When I run the app I receive the following error:
One or more errors occurred. (Cannot call Property for the property 'AppUser' on entity type 'MyTable' because it is configured as a navigation property. Property can only be used to configure scalar properties.)

It is fine when I remove the code:

            b.HasIndex(x => x.AppUser)
                .HasName("IX_eo_MyTable_AppUser");

Could please someone help me? Thanks.

Further technical details

EF Core version: 2.0.0
Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL 2.0.0
Operating system: Windows 10 Pro x64 (1703)
IDE: Visual Studio Community 2017

@smitpatel
Copy link
Member

@denis-pujdak-adm-it - Indexes can be defined on scalar properties only which would be mapped to a database column. You are trying to use HasIndex with a navigation property which is invalid and would throw exception as above. If your intention is to define an index on the foreignkey property used by the relationship then you need to call HasIndex on ForeignKey property and not the navigation.

You can configure foreignkey using HasForeignKey method when defining your relationship. And use the same property name for HasIndex call.
Given you class, it seems that you are using shadow property (you can use CLR property too if needed) the code would be something like this.

            b.HasOne(x => x.AppUser)
                .WithMany()
                .HasForeignKey("AppUserId") // Creates a column in database as AppUserId to store the FK value
                .IsRequired();

            b.HasIndex("AppUserId")
                .HasName("IX_eo_MyTable_AppUser");

@fairking
Copy link
Author

Thanks @smitpatel it's working. Cheers.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Oct 22, 2017
@smitpatel smitpatel reopened this Oct 22, 2017
@ajcvickers ajcvickers added type-enhancement and removed closed-no-further-action The issue is closed and no further action is planned. labels Oct 23, 2017
@ajcvickers ajcvickers added this to the 2.1.0 milestone Oct 23, 2017
@ajcvickers
Copy link
Member

Triage: we should throw a better message that doesn't reference "Property" since the app didn't call "Property" in this case.

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
AndriySvyryd added a commit that referenced this issue Feb 6, 2018
Update transient error message
Add sensitive data to the conceptual null exception
Don't validate mapping for shadow properties created by convention
Improve incompatible principal entity during fixup exception
Correct property is already a navigation exception

Fixes #8363
Fixes #8365
Fixes #9696
Fixes #9817
Fixes #10135
Fixes #10856
@AndriySvyryd AndriySvyryd removed their assignment Feb 6, 2018
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 6, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants