title: EF Core/Azure SQL DB: How to run migrations without deleting current data in production
body: <p>I have deployed a SQL database on Azure SQL database and published an API in .NET 5.0, however, EF Core/Database is an area I'm lacking a little knowledge in.</p>
  <p>Right now I have a lot of data in the database I had to input manually and I don't want to have to input everything again because I didn't ask the &quot;stupid question&quot;.</p>
  <p><strong>What I'm trying to do:</strong></p>
  <p>Be able to run migrations anytime whether I'm modifying a table or creating a new one
  without deleting data in production</p>
  <p><strong>Problem:</strong></p>
  <p>the Seed Data is in the DbContext ( in OnModelCreating )
  and have been added to the first migration.
  Before I apply the new migration I've generated the Migration Script twice,
  once with the seed data uncommented and once commented.</p>
  <p>THE NEW TABLE I'M TRYING TO ADD IS <code>Sectors</code> and there is no Seed Data for it.</p>
  <p><strong>AppDbContext.cs</strong></p>
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;...&gt;().HasData(new Device
          {
              // ...
          });
      //
      // All the seed data is added this way
      //
  }
  </code></pre>
  <p><strong>Uncommented:</strong></p>
  <p>Up: Adds new tables and doesn't do anything about seed data ( just some hours from StoreHours table, confusing )</p>
  <p>Down: Drops new tables</p>
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore.Migrations;
  
  namespace App.Persistence.Migrations
  {
  public partial class SeedDataUncommented: Migration
  {
      protected override void Up(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.AddColumn&lt;Guid&gt;(
              name: &quot;SectorId&quot;,
              table: &quot;Places&quot;,
              type: &quot;uniqueidentifier&quot;,
              nullable: true);
  
          migrationBuilder.CreateTable(
              name: &quot;Sectors&quot;,
              columns: table =&gt; new
              {
                  SectorId = table.Column&lt;Guid&gt;(type: &quot;uniqueidentifier&quot;, nullable: false),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
                  City = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
                  CreatedDateTime = table.Column&lt;DateTime&gt;(type: &quot;datetime2&quot;, nullable: false),
                  CreatedBy = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_Sectors&quot;, x =&gt; x.SectorId);
              });
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;25c68145-c5a3-438d-8cd4-acaec481384d&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2022, 1, 22, 2, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2022, 1, 22, 17, 0, 0, 0, DateTimeKind.Unspecified) });
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;3ad512e7-1aca-4339-9341-9b4028bb0536&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2022, 1, 22, 2, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2022, 1, 22, 17, 0, 0, 0, DateTimeKind.Unspecified) });
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;3f99ba19-f6e5-4e5d-afcf-d10f0ebc6009&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2022, 1, 22, 22, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2022, 1, 22, 11, 30, 0, 0, DateTimeKind.Unspecified) });
  
          //
          // A bunch of other migrationBuilder.UpdateData( table: &quot;StoreHours&quot;) I think because the Seed Data generates a new DateTime object everytime
          //
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_Places_SectorId&quot;,
              table: &quot;Places&quot;,
              column: &quot;SectorId&quot;);
  
          migrationBuilder.AddForeignKey(
              name: &quot;FK_Places_Sectors_SectorId&quot;,
              table: &quot;Places&quot;,
              column: &quot;SectorId&quot;,
              principalTable: &quot;Sectors&quot;,
              principalColumn: &quot;SectorId&quot;,
              onDelete: ReferentialAction.Restrict);
      }
  
      protected override void Down(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DropForeignKey(
              name: &quot;FK_Places_Sectors_SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;Sectors&quot;);
  
          migrationBuilder.DropIndex(
              name: &quot;IX_Places_SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.DropColumn(
              name: &quot;SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;25c68145-c5a3-438d-8cd4-acaec481384d&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2021, 11, 10, 2, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2021, 11, 10, 17, 0, 0, 0, DateTimeKind.Unspecified) });
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;3ad512e7-1aca-4339-9341-9b4028bb0536&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2021, 11, 10, 2, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2021, 11, 10, 17, 0, 0, 0, DateTimeKind.Unspecified) });
  
          migrationBuilder.UpdateData(
              table: &quot;StoreHours&quot;,
              keyColumn: &quot;StoreHourId&quot;,
              keyValue: new Guid(&quot;3f99ba19-f6e5-4e5d-afcf-d10f0ebc6009&quot;),
              columns: new[] { &quot;CloseHour&quot;, &quot;OpenHour&quot; },
              values: new object[] { new DateTime(2021, 11, 10, 22, 0, 0, 0, DateTimeKind.Unspecified), new DateTime(2021, 11, 10, 11, 30, 0, 0, DateTimeKind.Unspecified) });
  
          //
          // A bunch of other migrationBuilder.UpdateData( table: &quot;StoreHours&quot;) I think because the Seed Data generates a new DateTime object everytime
          //
      }
  }
  </code></pre>
  <p>}</p>
  <p><strong>Commented:</strong></p>
  <p>Up: Deletes all the seed data</p>
  <p>Down: Runs migrationBuilder.InsertData() for every table that was from the seed data.</p>
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore.Migrations;
  using NetTopologySuite.Geometries;
  
  namespace App.Persistence.Migrations
  {
  public partial class SeedDataCommented : Migration
  {
      protected override void Up(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DeleteData(
              table: &quot;Addresses&quot;,
              keyColumn: &quot;AddressId&quot;,
              keyValue: new Guid(&quot;16121702-bdf9-4444-a8b6-b08b33bb8eec&quot;));
  
          migrationBuilder.DeleteData(
              table: &quot;Addresses&quot;,
              keyColumn: &quot;AddressId&quot;,
              keyValue: new Guid(&quot;2de4a3c8-a391-4f00-8d10-c06b5bdb7629&quot;));
  
          migrationBuilder.DeleteData(
              table: &quot;Categories&quot;,
              keyColumn: &quot;CategoryId&quot;,
              keyValue: new Guid(&quot;066f36ea-bbbb-4243-e55d-08d91f53840e&quot;));
  
          migrationBuilder.DeleteData(
              table: &quot;Categories&quot;,
              keyColumn: &quot;CategoryId&quot;,
              keyValue: new Guid(&quot;e53265e0-a52b-46f0-abb2-08d91f5485bc&quot;));
  
          //
          // migrationBuilder.DeleteData() for every previously added Seed Data
          //
  
          migrationBuilder.AddColumn&lt;Guid&gt;(
              name: &quot;SectorId&quot;,
              table: &quot;Places&quot;,
              type: &quot;uniqueidentifier&quot;,
              nullable: true);
  
          migrationBuilder.CreateTable(
              name: &quot;Sectors&quot;,
              columns: table =&gt; new
              {
                  SectorId = table.Column&lt;Guid&gt;(type: &quot;uniqueidentifier&quot;, nullable: false),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
                  City = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: false),
                  CreatedDateTime = table.Column&lt;DateTime&gt;(type: &quot;datetime2&quot;, nullable: false),
                  CreatedBy = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_Sectors&quot;, x =&gt; x.SectorId);
              });
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_Places_SectorId&quot;,
              table: &quot;Places&quot;,
              column: &quot;SectorId&quot;);
  
          migrationBuilder.AddForeignKey(
              name: &quot;FK_Places_Sectors_SectorId&quot;,
              table: &quot;Places&quot;,
              column: &quot;SectorId&quot;,
              principalTable: &quot;Sectors&quot;,
              principalColumn: &quot;SectorId&quot;,
              onDelete: ReferentialAction.Restrict);
      }
  
      protected override void Down(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DropForeignKey(
              name: &quot;FK_Places_Sectors_SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;Sectors&quot;);
  
          migrationBuilder.DropIndex(
              name: &quot;IX_Places_SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.DropColumn(
              name: &quot;SectorId&quot;,
              table: &quot;Places&quot;);
  
          migrationBuilder.InsertData(
              table: &quot;Categories&quot;,
              columns: new[] { &quot;CategoryId&quot;, &quot;CreatedBy&quot;, &quot;CreatedDateTime&quot;, &quot;ImageUrl&quot;, &quot;Name&quot; },
              values: new object[,]
              {
                  // Seed Data
              });
  
          //
          // a bunch of other migrationBuilder.InsertData() for all the Seed Data
          //
      }
  }
  </code></pre>
  <p>}</p>
  <p>In both cases when I run <code>Add-Migration</code> it throws a warning message saying &quot;may result in the loss of data&quot;</p>
  <p>How should I do it?</p>