title: EFCore adding an Index could result in data loss?
body: <p>I am trying to add an Index to an existing EF Core Code First database, however I am getting a message saying that the scaffold may result in data loss - I have had a look through the migration and the SQL but it is not clear if it will - any ideas?</p>
  <p>I have the following Model:</p>
  <pre><code>public class Likes
  {
      public int ID { get; set; }
  
      public string Person { get; set; }
  
      public string JudgedPerson { get; set; }
  
      public int IsLike { get; set; }
  
      public DateTime Date { get; set; }
  }
  </code></pre>
  <p>Person and JudgedPerson are GUIDs and I want to add an Index to these columns to speed up data access.</p>
  <p>I have added the following in my AppDbContext OnModelCreating method to specify the index:</p>
  <pre><code>        builder.Entity&lt;Likes&gt;()
              .HasIndex(p =&gt; new { p.Person, p.JudgedPerson });
  </code></pre>
  <p>I have then run <strong>dotnet ef migrations add</strong>  --&gt; This then gives the following message:</p>
  <blockquote>
  <p>An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.</p>
  </blockquote>
  <p>The resulting Migration code is showing:</p>
  <pre><code>public partial class AddIndexToLikesPersonJudgedPerson : Migration
  {
     protected override void Up(MigrationBuilder migrationBuilder)
     {
          migrationBuilder.AlterColumn&lt;string&gt;(
              name: &quot;Person&quot;,
              table: &quot;Likes&quot;,
              nullable: true,
              oldClrType: typeof(string),
              oldType: &quot;nvarchar(max)&quot;,
              oldNullable: true);
  
          migrationBuilder.AlterColumn&lt;string&gt;(
              name: &quot;JudgedPerson&quot;,
              table: &quot;Likes&quot;,
              nullable: true,
              oldClrType: typeof(string),
              oldType: &quot;nvarchar(max)&quot;,
              oldNullable: true);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_Likes_Person_JudgedPerson&quot;,
              table: &quot;Likes&quot;,
              columns: new[] { &quot;Person&quot;, &quot;JudgedPerson&quot; });
      }
  
      protected override void Down(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DropIndex(
              name: &quot;IX_Likes_Person_JudgedPerson&quot;,
              table: &quot;Likes&quot;);
  
          migrationBuilder.AlterColumn&lt;string&gt;(
              name: &quot;Person&quot;,
              table: &quot;Likes&quot;,
              type: &quot;nvarchar(max)&quot;,
              nullable: true,
              oldClrType: typeof(string),
              oldNullable: true);
  
          migrationBuilder.AlterColumn&lt;string&gt;(
              name: &quot;JudgedPerson&quot;,
              table: &quot;Likes&quot;,
              type: &quot;nvarchar(max)&quot;,
              nullable: true,
              oldClrType: typeof(string),
              oldNullable: true);
      }
  }
  </code></pre>
  <p>When I script this out, I get the following SQL:</p>
  <pre><code>DECLARE @var0 sysname;
  SELECT @var0 = [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'[Likes]') AND [c].[name] = N'Person');
  IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Likes] DROP CONSTRAINT [' + @var0 + '];');
  ALTER TABLE [Likes] ALTER COLUMN [Person] nvarchar(450) NULL;
  
  GO
  
  DECLARE @var1 sysname;
  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'[Likes]') AND [c].[name] = N'JudgedPerson');
  IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [Likes] DROP CONSTRAINT [' + @var1 + '];');
  ALTER TABLE [Likes] ALTER COLUMN [JudgedPerson] nvarchar(450) NULL;
  
  GO
  
  CREATE INDEX [IX_Likes_Person_JudgedPerson] ON [Likes] ([Person], [JudgedPerson]);
  
  GO
  
  INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
  VALUES (N'20211220123038_AddIndexToLikesPersonJudgedPerson', N'3.1.10');
  
  GO
  </code></pre>