Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Missing Method HasIndex

I've just started to migrate to a SQL database and running into a problem with a MissingMethodException being thrown. Here is the configuration class that is throwing the error:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Project.Core.Entities.Users;

namespace Project.Persistance.Configuration.Users
{
    public class UserClaimTypeConfiguration : IEntityTypeConfiguration<UserClaimType>
    {
        public void Configure(EntityTypeBuilder<UserClaimType> builder)
        {
            builder.HasKey(entity => entity.Id);

            builder.Property(entity => entity.Name)
                        .IsRequired()
                        .HasMaxLength(30);

            builder.HasIndex(entity => entity.Name);

            builder.Property(entity => entity.Description)
                        .IsRequired(false)
                        .HasMaxLength(100);

            builder.Ignore(entity => entity.ValueType);
        }
    }
}

And this is error I am getting in the console:

PM> add-migration user
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method not found: 'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex(System.Linq.Expressions.Expression`1<System.Func`2<!0,System.Object>>)'.
   at Project.Persistance.Configuration.Users.UserClaimTypeConfiguration.Configure(EntityTypeBuilder`1 builder)
   at Microsoft.EntityFrameworkCore.ModelBuilder.ApplyConfiguration[TEntity](IEntityTypeConfiguration`1 configuration)

I've tried 'Goggle' and the Microsoft docs but can't seem to find any reference to this issue - so it must be my setup. Just can't figure out what is causing it!

like image 294
Adam Avatar asked Jul 02 '19 18:07

Adam


Video Answer


2 Answers

Breaking change in .NET Core 3.0 preview 3. Fixed in preview 4: ASP.NET Core Issue 8467 (RESOLVED)

like image 175
Adam Avatar answered Oct 13 '22 21:10

Adam


Based on this document:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.entitytypebuilder-1.hasindex?view=efcore-3.1

Implementation of this method is done in Microsoft.EntityFrameworkCore versions 3.1 3.0 2.2 2.1 2.0 1.1 1.0

You have to check your Microsoft.EntityFrameworkCore version. It should be one of the above versions. If it does not work for a versoin try another.

like image 28
Mehdi Lotfi Avatar answered Oct 13 '22 19:10

Mehdi Lotfi