title: dotnet ef migrations: Invalid column name when it has just been added
body: <p>So apparently, when I add a new model and relations to another existing model, it won't scaffold the migration properly. Even though a migrations file will be created, a chunk load of errors will be shown.</p>
  
  <pre><code>fail: Microsoft.EntityFrameworkCore.Query[10100]
        An exception occurred in the database while iterating the results of a query for context type 'CounterCore.Data.CounterDbContext'.
        System.NullReferenceException: Object reference not set to an instance of an object.
           at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.System.IObserver&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.Object&gt;&gt;.OnNext(KeyValuePair`2 keyValuePair)
           at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
           at Microsoft.EntityFrameworkCore.Internal.CoreLoggerExtensions.QueryIterationFailed(IDiagnosticsLogger`1 diagnostics, Type contextType, Exception exception)
           at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
           at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;ExecuteSingletonAsyncQuery&gt;d__23`1.MoveNext()
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.System.IObserver&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.Object&gt;&gt;.OnNext(KeyValuePair`2 keyValuePair)
     at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
     at Microsoft.EntityFrameworkCore.Internal.CoreLoggerExtensions.QueryIterationFailed(IDiagnosticsLogger`1 diagnostics, Type contextType, Exception exception)
     at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;ExecuteSingletonAsyncQuery&gt;d__23`1.MoveNext()
  fail: Microsoft.EntityFrameworkCore.Query[10100]
        An exception occurred in the database while iterating the results of a query for context type 'CounterCore.Data.CounterDbContext'.
        System.NullReferenceException: Object reference not set to an instance of an object.
           at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.System.IObserver&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.Object&gt;&gt;.OnNext(KeyValuePair`2 keyValuePair)
           at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
           at Microsoft.EntityFrameworkCore.Internal.CoreLoggerExtensions.QueryIterationFailed(IDiagnosticsLogger`1 diagnostics, Type contextType, Exception exception)
           at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
           at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
           at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
           at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
           at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;ExecuteSingletonAsyncQuery&gt;d__23`1.MoveNext()
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.System.IObserver&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.Object&gt;&gt;.OnNext(KeyValuePair`2 keyValuePair)
     at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
     at Microsoft.EntityFrameworkCore.Internal.CoreLoggerExtensions.QueryIterationFailed(IDiagnosticsLogger`1 diagnostics, Type contextType, Exception exception)
     at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;ExecuteSingletonAsyncQuery&gt;d__23`1.MoveNext()
  An exception occurred in the database while iterating the results of a query for context type 'CounterCore.Data.CounterDbContext'.
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.System.IObserver&lt;System.Collections.Generic.KeyValuePair&lt;System.String,System.Object&gt;&gt;.OnNext(KeyValuePair`2 keyValuePair)
     at System.Diagnostics.DiagnosticListener.Write(String name, Object value)
     at Microsoft.EntityFrameworkCore.Internal.CoreLoggerExtensions.QueryIterationFailed(IDiagnosticsLogger`1 diagnostics, Type contextType, Exception exception)
     at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.&lt;MoveNext&gt;d__5.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.&lt;ExecuteSingletonAsyncQuery&gt;d__23`1.MoveNext()
  </code></pre>
  
  <p>So here's what I did</p>
  
  <p>User class:</p>
  
  <pre><code>public class User : IdentityUser&lt;long&gt;
  {
      [MaxLength(20)]
      public string ReferralCode { get; set; }
  
      public ICollection&lt;UserReferral&gt; Referrals { get; set; }
  }
  </code></pre>
  
  <p>UserReferral Class:</p>
  
  <pre><code>public class UserReferral : BaseEntityModel
      {
          public long ReferredUserId { get; set; }
  
          public string ReferralCode { get; set; }
  
          public virtual User ReferredUser { get; set; }
      }
  </code></pre>
  
  <p>Within DbContext:
  I simply added a new entity for user referral,</p>
  
  <pre><code>            builder.Entity&lt;UserReferral&gt;(entity =&gt;
              {
                  entity.HasKey(ur =&gt; ur.ReferredUserId).HasName("UserReferral_PK_Id");
  
                  entity.HasOne(ur =&gt; ur.ReferredUser).WithMany().HasForeignKey(ur =&gt; ur.ReferredUserId).OnDelete(DeleteBehavior.Restrict);
              });
  </code></pre>
  
  <p>And a property for the User entity to generate the string randomly on create. And trust me, I tried to fix this 5-10 times over 3 days so I've tried restructuring countless times.</p>
  
  <pre><code>entity.HasAlternateKey(u =&gt; u.ReferralCode).HasName("User_AK_ReferralCode");
                  entity.Property(u =&gt; u.ReferralCode).HasDefaultValue(RandomGenerator.GenerateString());
  </code></pre>
  
  <p>DbContext for relations are done as usual, first time facing this issue. </p>