简体   繁体   English

EF Core 试图删除不存在的索引

[英]EF Core trying to drop index that doesn't exist

Experiencing an error when running a migration attempting to modify a column from being nullable to not-null.运行迁移尝试将列从可为 null 修改为 not-null 时遇到错误。 My model class looks like this:我的 model class 看起来像这样:

public class SaleLandNonDeeded
{
    public Guid SaleLandNonDeededId { get; set; }
    public Guid SaleId { get; set; }
    public decimal? TotalValue { get; set; }

    public virtual Sale Sale { get; set; }
}

Previously, SaleId was of type Guid?以前, SaleIdGuid? . . Here is the model configuration in the DbContext :这是 DbContext 中的DbContext配置:

modelBuilder.Entity<SaleLandNonDeeded>(entity =>
{
    entity.Property(e => e.SaleLandNonDeededId).HasDefaultValueSql("(newid())");

    entity.Property(e => e.TotalValue).HasColumnType("money");

    entity.HasOne(d => d.Sale)
        .WithMany(p => p.SaleLandNonDeeded)
        .HasForeignKey(d => d.SaleId)
        .HasConstraintName("FK_SaleLandNonDeeded_SaleId");
});

After changing the type, a new migration was created:更改类型后,创建了一个新的迁移:

// This line was added manually to delete null values before changing
// the column type.
migrationBuilder.Sql("DELETE FROM SaleLandNonDeeded WHERE SaleId IS NULL");

migrationBuilder.AlterColumn<Guid>(
    name: "SaleId",
    table: "SaleLandNonDeeded",
    type: "uniqueidentifier",
    nullable: false,
    defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
    oldClrType: typeof(Guid),
    oldType: "uniqueidentifier",
    oldNullable: true);

This fails with the following:这失败并出现以下情况:

Microsoft.EntityFrameworkCore.Database.Command: Error: Failed executing DbCommand (34ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] DROP INDEX [IX_SaleLandNonDeeded_SaleId] ON [SaleLandNonDeeded]; Microsoft.EntityFrameworkCore.Database.Command:错误:执行 DbCommand 失败(34 毫秒)[Parameters=[],CommandType='Text',CommandTimeout='30'] DROP INDEX [IX_SaleLandNonDeeded_SaleId] ON [SaleLandNonDeeded]; DECLARE @var1 sysname;声明@var1 系统名; SELECT @var1 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[SaleLandNonDeeded]') AND [c].[name] = N'SaleId'); SELECT @var1 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[SaleLandNonDeeded]') AND [c].[name] = N'SaleId'); IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [SaleLandNonDeeded] DROP CONSTRAINT [' + @var1 + '];'); IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [SaleLandNonDeeded] DROP CONSTRAINT [' + @var1 + '];'); ALTER TABLE [SaleLandNonDeeded] ALTER COLUMN [SaleId] uniqueidentifier NOT NULL; ALTER TABLE [SaleLandNonDeeded] ALTER COLUMN [SaleId] uniqueidentifier NOT NULL; ALTER TABLE [SaleLandNonDeeded] ADD DEFAULT '00000000-0000-0000-0000-000000000000' FOR [SaleId];更改表 [SaleLandNonDeeded] 添加默认值 '00000000-0000-0000-0000-000000000000' FOR [SaleId]; CREATE INDEX [IX_SaleLandNonDeeded_SaleId] ON [SaleLandNonDeeded] ([SaleId]);在 [SaleLandNonDeeded] ([SaleId]) 上创建索引 [IX_SaleLandNonDeeded_SaleId]; Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in System.Private.CoreLib.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in System.Private.CoreLib.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in System.Private.CoreLib.dll An exception of type 'Microsoft.Data.SqlClient.SqlException' occurred in System.Private.CoreLib.dll but was not handled in user code Cannot drop the index 'SaleLandNonDeeded.IX_SaleLandNonDeeded_SaleId', because it does not exist or you do not have permission.抛出的异常:System.Private.CoreLib.dll 中的“Microsoft.Data.SqlClient.SqlException” SqlException' in System.Private.CoreLib.dll System.Private.CoreLib.dll 中出现类型为'Microsoft.Data.SqlClient.SqlException' 的异常,但未在用户代码中处理 无法删除索引'SaleLandNonDeeded.IX_SaleLandNonDeeded_SaleId',因为它不存在或您没有权限。

There is no IX_SaleLandNonDeeded_SaleId anywhere in the model, context, migration, or snapshot. model、上下文、迁移或快照中的任何位置都没有IX_SaleLandNonDeeded_SaleId Doing a Ctrl+F on the Entire Solution returns no matches except for the above log entries.对整个解决方案执行 Ctrl+F 后,除上述日志条目外,不会返回任何匹配项。 I do not know why EF Core is attempting to drop and create this index.我不知道为什么 EF Core 试图删除并创建这个索引。 I did find an issue pertaining to this here: https://github.com/do.net/efcore/issues/7535 which was way back in EF Core 1.0 and closed as fixed.我确实在这里发现了一个与此相关的问题: https://github.com/do.net/efcore/issues/7535回到 EF Core 1.0 并已修复关闭。 I am using EF Core 5.0.我正在使用 EF Core 5.0。

Definitely not the cleanest answer, but I had the same issue as you and I just added the missing index as the first line of the migration:绝对不是最干净的答案,但我和你有同样的问题,我只是将丢失的索引添加为迁移的第一行:

migrationBuilder.CreateIndex("IX_SaleLandNonDeeded_SaleId", "SaleLandNonDeeded", "SaleId");

Don't forget to also update the Down() method:不要忘记更新Down()方法:

migrationBuilder.DropIndex("IX_SaleLandNonDeeded_SaleId", "SaleLandNonDeeded");

I had upgraded from EF 6 to EF core 5.0.我已经从 EF 6 升级到 EF core 5.0。 Don't ask me why they decided to change naming conventions.不要问我为什么他们决定改变命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM