title: EF Core Migration recreates renamed tables
body: <p>I'm using .NET 5 Identity package and renamed it's tables as well as used an extension of <code>IdentityUser</code>. Now I decided to add few properties to my <code>User</code> and apply a migration, but the migration tries to recreate my renamed tables.</p>
  <p>Here is my User model:</p>
  <pre><code>public class User : IdentityUser&lt;int&gt;, IUser
  {
      public string Password { get; set; }
  
      public string Username { get =&gt; base.UserName; set =&gt; base.UserName = value; }
      public string Name { get; set; }
  
      public bool PubliclyVisible { get; set; } //New
      public IEnumerable&lt;User&gt; Trackers { get; set; } //New
      public IEnumerable&lt;User&gt; Tracked { get; set; } //New
  
  }
  </code></pre>
  <p>My <code>DbContext</code> extension model configuration:</p>
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
  
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity&lt;User&gt;(b =&gt;
      {
          b.ToTable(&quot;Users&quot;);
          b.HasMany(b =&gt; b.Tracked); //New
          b.HasMany(b =&gt; b.Trackers); //New 
          b.Ignore(u =&gt; u.Username);
                  
      });
  
      modelBuilder.Entity&lt;GPSData&gt;().HasOne(d =&gt; d.User);
  
      modelBuilder.Entity&lt;IdentityUserClaim&lt;int&gt;&gt;(b =&gt; b.ToTable(&quot;UserClaims&quot;));
      modelBuilder.Entity&lt;IdentityUserLogin&lt;int&gt;&gt;(b =&gt; b.ToTable(&quot;UserLogins&quot;));
      modelBuilder.Entity&lt;IdentityUserToken&lt;int&gt;&gt;(b =&gt; b.ToTable(&quot;UserTokens&quot;));
      modelBuilder.Entity&lt;IdentityRole&gt;(b =&gt; b.ToTable(&quot;Roles&quot;));
      modelBuilder.Entity&lt;IdentityRoleClaim&lt;int&gt;&gt;(b =&gt; b.ToTable(&quot;RoleClaims&quot;));
      modelBuilder.Entity&lt;IdentityUserRole&lt;int&gt;&gt;(b =&gt; b.ToTable(&quot;UserRoles&quot;));
  
  }
  </code></pre>
  <p>I marked the new properties in my <code>User</code> as well as additional lines in the configuration with <code>//New</code>.</p>
  <p>After I run <code>add-migration AddedTrackingProperties</code></p>
  <p>I get the following migration that tries to recreate my tables and thus I get an error that <code>AspNetUserRoles</code> already exist, when I update the table.</p>
  <p>Migration config</p>
  <pre><code>public partial class AddedTrackingProperties : Migration
  {
      protected override void Up(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.CreateTable(
              name: &quot;AspNetRoles&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                      .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  NormalizedName = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  ConcurrencyStamp = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_AspNetRoles&quot;, x =&gt; x.Id);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;Roles&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  NormalizedName = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  ConcurrencyStamp = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_Roles&quot;, x =&gt; x.Id);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;Users&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                      .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                  Password = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  PubliclyVisible = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                  UserName = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  NormalizedUserName = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  Email = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  NormalizedEmail = table.Column&lt;string&gt;(type: &quot;nvarchar(256)&quot;, maxLength: 256, nullable: true),
                  EmailConfirmed = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                  PasswordHash = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  SecurityStamp = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  ConcurrencyStamp = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  PhoneNumber = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  PhoneNumberConfirmed = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                  TwoFactorEnabled = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                  LockoutEnd = table.Column&lt;DateTimeOffset&gt;(type: &quot;datetimeoffset&quot;, nullable: true),
                  LockoutEnabled = table.Column&lt;bool&gt;(type: &quot;bit&quot;, nullable: false),
                  AccessFailedCount = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_Users&quot;, x =&gt; x.Id);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;RoleClaims&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                      .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                  RoleId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  ClaimType = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  ClaimValue = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_RoleClaims&quot;, x =&gt; x.Id);
                  table.ForeignKey(
                      name: &quot;FK_RoleClaims_AspNetRoles_RoleId&quot;,
                      column: x =&gt; x.RoleId,
                      principalTable: &quot;AspNetRoles&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;GPSData&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                      .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                  Longitude = table.Column&lt;double&gt;(type: &quot;float&quot;, nullable: false),
                  Latitude = table.Column&lt;double&gt;(type: &quot;float&quot;, nullable: false),
                  Speed = table.Column&lt;double&gt;(type: &quot;float&quot;, nullable: false),
                  Timestamp = table.Column&lt;DateTime&gt;(type: &quot;datetime2&quot;, nullable: false),
                  UserId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_GPSData&quot;, x =&gt; x.Id);
                  table.ForeignKey(
                      name: &quot;FK_GPSData_Users_UserId&quot;,
                      column: x =&gt; x.UserId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;UserClaims&quot;,
              columns: table =&gt; new
              {
                  Id = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
                      .Annotation(&quot;SqlServer:Identity&quot;, &quot;1, 1&quot;),
                  UserId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  ClaimType = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  ClaimValue = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_UserClaims&quot;, x =&gt; x.Id);
                  table.ForeignKey(
                      name: &quot;FK_UserClaims_Users_UserId&quot;,
                      column: x =&gt; x.UserId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;UserLogins&quot;,
              columns: table =&gt; new
              {
                  LoginProvider = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
                  ProviderKey = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
                  ProviderDisplayName = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
                  UserId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_UserLogins&quot;, x =&gt; new { x.LoginProvider, x.ProviderKey });
                  table.ForeignKey(
                      name: &quot;FK_UserLogins_Users_UserId&quot;,
                      column: x =&gt; x.UserId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;UserRoles&quot;,
              columns: table =&gt; new
              {
                  UserId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  RoleId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_UserRoles&quot;, x =&gt; new { x.UserId, x.RoleId });
                  table.ForeignKey(
                      name: &quot;FK_UserRoles_AspNetRoles_RoleId&quot;,
                      column: x =&gt; x.RoleId,
                      principalTable: &quot;AspNetRoles&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
                  table.ForeignKey(
                      name: &quot;FK_UserRoles_Users_UserId&quot;,
                      column: x =&gt; x.UserId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;UserTokens&quot;,
              columns: table =&gt; new
              {
                  UserId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  LoginProvider = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
                  Name = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
                  Value = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_UserTokens&quot;, x =&gt; new { x.UserId, x.LoginProvider, x.Name });
                  table.ForeignKey(
                      name: &quot;FK_UserTokens_Users_UserId&quot;,
                      column: x =&gt; x.UserId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  
          migrationBuilder.CreateTable(
              name: &quot;UserUser&quot;,
              columns: table =&gt; new
              {
                  TrackedId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  TrackersId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_UserUser&quot;, x =&gt; new { x.TrackedId, x.TrackersId });
                  table.ForeignKey(
                      name: &quot;FK_UserUser_Users_TrackedId&quot;,
                      column: x =&gt; x.TrackedId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
                  table.ForeignKey(
                      name: &quot;FK_UserUser_Users_TrackersId&quot;,
                      column: x =&gt; x.TrackersId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Restrict);
              });
  
          migrationBuilder.CreateIndex(
              name: &quot;RoleNameIndex&quot;,
              table: &quot;AspNetRoles&quot;,
              column: &quot;NormalizedName&quot;,
              unique: true,
              filter: &quot;[NormalizedName] IS NOT NULL&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_GPSData_UserId&quot;,
              table: &quot;GPSData&quot;,
              column: &quot;UserId&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_RoleClaims_RoleId&quot;,
              table: &quot;RoleClaims&quot;,
              column: &quot;RoleId&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_UserClaims_UserId&quot;,
              table: &quot;UserClaims&quot;,
              column: &quot;UserId&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_UserLogins_UserId&quot;,
              table: &quot;UserLogins&quot;,
              column: &quot;UserId&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_UserRoles_RoleId&quot;,
              table: &quot;UserRoles&quot;,
              column: &quot;RoleId&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;EmailIndex&quot;,
              table: &quot;Users&quot;,
              column: &quot;NormalizedEmail&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;UserNameIndex&quot;,
              table: &quot;Users&quot;,
              column: &quot;NormalizedUserName&quot;,
              unique: true,
              filter: &quot;[NormalizedUserName] IS NOT NULL&quot;);
  
          migrationBuilder.CreateIndex(
              name: &quot;IX_UserUser_TrackersId&quot;,
              table: &quot;UserUser&quot;,
              column: &quot;TrackersId&quot;);
      }
  
      protected override void Down(MigrationBuilder migrationBuilder)
      {
          migrationBuilder.DropTable(
              name: &quot;GPSData&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;RoleClaims&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;Roles&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;UserClaims&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;UserLogins&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;UserRoles&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;UserTokens&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;UserUser&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;AspNetRoles&quot;);
  
          migrationBuilder.DropTable(
              name: &quot;Users&quot;);
      }
  }
  </code></pre>
  <p>So where is the mistake?</p>