title: .Net Core Migration System.ArgumentNullException: Value cannot be null
body: <p>This is my DbContext</p>
  
  <pre><code>public class DocumentStudioDbContext : DbContext
  {
      public DocumentStudioDbContext(DbContextOptions&lt;DocumentStudioDbContext&gt; options) : base(options) { }
      public DbSet&lt;Documents&gt; Documents { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.HasDefaultSchema(schema: Environment.GetEnvironmentVariable(DSCASGlobals.DS_DatabaseSchema));
          base.OnModelCreating(modelBuilder);
      }
  
      public override int SaveChanges()
      {
          Audit();
          return base.SaveChanges();
      }
  
      public async Task&lt;int&gt; SaveChangesAsync()
      {
          Audit();
          return await base.SaveChangesAsync();
      }
  
      private void Audit()
      {
          var entries = ChangeTracker.Entries().Where(x =&gt; x.Entity is Documents &amp;&amp; (x.State == EntityState.Added || x.State == EntityState.Modified));
          foreach (var entry in entries)
          {
              if (entry.State == EntityState.Added)
              {
                  ((Documents)entry.Entity).CreatedOn = DateTime.UtcNow;
              }
          ((Documents)entry.Entity).UpdatedOn = DateTime.UtcNow;
          }
      }
  }
  </code></pre>
  
  <p>When I try to run a migration, I get this error:</p>
  
  <blockquote>
    <p>System.ArgumentNullException: Value cannot be null. Parameter name:
    connectionString    at
    Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value,
    String parameterName)    at
    Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder
    optionsBuilder, String connectionString, Action<code>1
    sqlServerOptionsAction)    at
    DocumentStudio.Startup.&lt;&gt;c.&lt;ConfigureServices&gt;b__1_0(DbContextOptionsBuilder
    options) in
    C:\Users\amosa\source\repos\DotNetCore\DocumentStudio\DocumentStudio\Startup.cs:line
    48    at
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.&lt;&gt;c__DisplayClass1_0</code>2.b__0(IServiceProvider
    p, DbContextOptionsBuilder b)    at
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider
    applicationServiceProvider, Action<code>2 optionsAction)    at
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.&lt;&gt;c__DisplayClass10_0</code>1.b__0(IServiceProvider
    p)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite
    factoryCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;>c__DisplayClass1_0.b__0(ServiceProviderEngineScope
    scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
    serviceType, ServiceProviderEngineScope serviceProviderEngineScope)<br>
    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type
    serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
    provider, Type serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider
    provider)    at
    Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.&lt;>c__10<code>1.&lt;AddCoreServices&gt;b__10_1(IServiceProvider
    p)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite
    factoryCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite
    scopedCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite
    enumerableCallSite, ServiceProviderEngineScope scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSite(IServiceCallSite
    callSite, TArgument argument)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;>c__DisplayClass1_0.b__0(ServiceProviderEngineScope
    scope)    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type
    serviceType, ServiceProviderEngineScope serviceProviderEngineScope)<br>
    at
    Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type
    serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider
    provider, Type serviceType)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider
    provider)    at
    Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServices[T](IServiceProvider
    provider)    at
    Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes()
    at
    Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String
    name)    at
    Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String
    contextType)    at
    Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String
    name, String outputDir, String contextType)    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String
    name, String outputDir, String contextType)    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.&lt;>c__DisplayClass0_1.&lt;.ctor>b__0()
    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;>c__DisplayClass3_0`1.b__0()
    at
    Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action
    action) Value cannot be null. Parameter name: connectionString</p>
  </blockquote>
  
  <p>My ConnectionString comes from the Environment Variables that is set in Visual Studio. I am unsure of how I can perform a migration. </p>
  
  <p>What else do I need to do?</p>
  
  <p>EDIT:</p>
  
  <p>DbContext in Startup.cs</p>
  
  <pre><code>services.AddDbContext&lt;DocumentStudioDbContext&gt;(options =&gt; options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString)));
  </code></pre>
  
  <p>EDIT2: </p>
  
  <p><a href="https://i.stack.imgur.com/Q9AQa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q9AQa.png" alt="enter image description here"></a></p>
  
  <p>Managed to get the variable out from the environment variable</p>