微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EF Core不断抱怨不存在的索引

如何解决EF Core不断抱怨不存在的索引

在运行时,我有一堆日志从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','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','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','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在迁移过程中会自动创建。

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

可以安全忽略还是应该从我这边采取一些行动?

P.S。使用sql Server作为数据库提供程序在3.1.8上进行了测试。

解决方法

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

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

但这就是它。不为正导致另一个索引的列生成索引是完全可以的。您可以放心地忽略它,并通过在OnConfiguring覆盖中添加以下内容来抑制它:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。