EF核心缺失方法HasIndex

时间:2019-07-02 18:58:14

标签: asp.net-core entity-framework-core

我刚开始迁移到SQL数据库并遇到抛出MissingMethodException的问题。这是引发错误的配置类:

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);
        }
    }
}

这是我进入控制台的错误:

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)

我已经尝试过'Goggle'和Microsoft文档,但似乎找不到与此问题有关的任何参考-因此它必须是我的设置。只是不知道是什么原因造成的!

2 个答案:

答案 0 :(得分:2)

基于此文档:

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

此方法的实现在Microsoft.EntityFrameworkCore版本3.1 3.0 2.2 2.1 2.0 1.1 1.0

中进行

您必须检查Microsoft.EntityFrameworkCore版本。它应该是以上版本之一。如果它不适用于versoin,请尝试另一个。

答案 1 :(得分:0)

.NET Core 3.0预览版中的重大更改。在预览版4中已修复: ASP.NET Core Issue 8467 (RESOLVED)