【问题标题】:EF Core keeps complaining about non existent indexesEF Core 不断抱怨不存在的索引
【发布时间】:2021-01-01 23:57:37
【问题描述】:

我在运行时收到一堆来自 EF Core 的日志抱怨,如下所示:

[10:56:14 DBG] The index {'InvoiceId'} was not created on entity type 'CompleteCase' as the properties are already covered by the index {'InvoiceId', 'Code'}.
[10:56:14 DBG] The index {'ReportPeriodId'} was not created on entity type 'FixedBill' as the properties are already covered by the index {'ReportPeriodId', 'MedCompanyTypeId', 'FixedBillTypeId'}.
[10:56:14 DBG] The index {'CasePeriodId'} was not created on entity type 'Invoice' as the properties are already covered by the index {'CasePeriodId', 'ReportPeriodId', 'MedCompanyTypeId'}.
[10:56:14 DBG] The index {'InvoiceId'} was not created on entity type 'InvoiceFixedBillMap' as the properties are already covered by the index {'InvoiceId', 'FixedBillId'}.
[10:56:14 DBG] The index {'MedCompanyDepartmentTypeId'} was not created on entity type 'MedCompanyDepartmentDoctorMap' as the properties are already covered by the index {'MedCompanyDepartmentTypeId', 'MedCompanyDoctorTypeId', 'DoctorSpecialtyTypeId', 'StartDate', 'EndDate'}.
[10:56:14 DBG] The index {'VMPMethodTypeId'} was not created on entity type 'VMPMethodMKBMap' as the properties are already covered by the index {'VMPMethodTypeId', 'MKBTypeId'}.
[10:56:14 DBG] The index {'KSGBaseRateTypeId'} was not created on entity type 'KSGBaseRate' as the properties are already covered by the index {'KSGBaseRateTypeId', 'StartDate', 'EndDate'}.
[10:56:14 DBG] The index {'KSGTypeId'} was not created on entity type 'KSGIndexRate' as the properties are already covered by the index {'KSGTypeId', 'KSGIndexTypeId', 'StartDate', 'EndDate'}.
[10:56:14 DBG] The index {'MedCompanyTypeId'} was not created on entity type 'MedCompanyKSGIndexRate' as the properties are already covered by the index {'MedCompanyTypeId', 'MedCompanyUnitTypeId', 'MedCompanyDepartmentTypeId', 'UMPTypeId', 'KSGIndexTypeId', 'StartDate', 'EndDate'}.
[10:56:14 DBG] The index {'UserId'} was not created on entity type 'UserRole' as the properties are already covered by the index {'UserId', 'RoleId'}.
[10:56:14 DBG] The index {'UserId'} was not created on entity type 'IdentityUserToken<Guid>' as the properties are already covered by the index {'UserId', 'LoginProvider', 'Name'}.

我可以看到这些的原因 - 因为有使用这些列的复合索引,它们使单独的索引无用。这些列都是外键,EF Core 在迁移过程中自动创建。

但是这些索引实际上都不存在于迁移或数据库本身中(代码优先)。我已经非常仔细地检查了。甚至删除了所有迁移并从头开始创建它们 - 没有。但是在运行时我每次都会得到这些。

是否可以忽略或者我应该采取一些行动?

附:使用 SQL Server 作为数据库提供程序在 3.1.8 上测试。

【问题讨论】:

    标签: sql-server entity-framework-core foreign-keys relational-database entity-framework-migrations


    【解决方案1】:

    但是这些索引实际上都不存在于迁移或数据库本身中(代码优先)。

    当然不是,您在此处看到的只是一个警告,告诉您某些索引已从模型中删除(忽略)。可能令人困惑的部分是您没有明确请求此类索引,正如您所说,它们是由 EF 自动创建的(顺便说一句,不能从外部删除)所以 EF Core 不应该警告您。

    但事实就是如此。不为另一个索引前导的列生成索引是完全可以的。您可以放心地忽略它,并通过将以下内容添加到 OnConfiguring 覆盖来抑制它:

    optionsBuilder.ConfigureWarnings(warnings => warnings
        .Ignore(CoreEventId.RedundantIndexRemoved);
    

    【讨论】:

    • 谢谢。配置日志警告非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多