.Net Core迁移System.ArgumentNullException:值不能为空

时间:2019-06-19 03:33:52

标签: c# asp.net .net asp.net-core

这是我的DbContext

public class DocumentStudioDbContext : DbContext
{
    public DocumentStudioDbContext(DbContextOptions<DocumentStudioDbContext> options) : base(options) { }
    public DbSet<Documents> 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<int> SaveChangesAsync()
    {
        Audit();
        return await base.SaveChangesAsync();
    }

    private void Audit()
    {
        var entries = ChangeTracker.Entries().Where(x => x.Entity is Documents && (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;
        }
    }
}

当我尝试运行迁移时,出现此错误:

  

System.ArgumentNullException:值不能为null。参数名称:   connectionString在   Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值,   字符串parameterName)在   Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder   optionsBuilder,字符串连接字符串,操作1 sqlServerOptionsAction) at DocumentStudio.Startup.<>c.<ConfigureServices>b__1_0(DbContextOptionsBuilder options) in C:\Users\amosa\source\repos\DotNetCore\DocumentStudio\DocumentStudio\Startup.cs:line 48 at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_0 2.b__0(IServiceProvider   p,DbContextOptionsBuilder b)在   Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory [TContext](IServiceProvider   applicationServiceProvider,操作2 optionsAction) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass10_0 1.b__0(IServiceProvider   p)在   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite   factoryCallSite,ServiceProviderEngineScope范围)   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite   callSite,TArgument参数)在   Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine。<> c__DisplayClass1_0.b__0(ServiceProviderEngineScope   范围)   Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type   serviceType,ServiceProviderEngineScope serviceProviderEngineScope)
  在   Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type   serviceType)在   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider   提供者,输入serviceType)   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](IServiceProvider   提供者)   Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions。<> c__10 1.<AddCoreServices>b__10_1(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite   callSite,TArgument参数)在   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite   scopedCallSite,ServiceProviderEngineScope范围)   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor 2.VisitCallSite(IServiceCallSite   callSite,TArgument参数)在   Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine。<> c__DisplayClass1_0.b__0(ServiceProviderEngineScope   范围)   Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type   serviceType,ServiceProviderEngineScope serviceProviderEngineScope)
  在   Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type   serviceType)在   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider   提供者,输入serviceType)   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](IServiceProvider   提供者)   Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetServices [T](IServiceProvider   提供者)   Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes()   在   Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String   名称)   Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String   contextType)   Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String   名称,字符串outputDir,字符串contextType)位于   Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String   名称,字符串outputDir,字符串contextType)位于   Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration。<> c__DisplayClass0_1。<。ctor> b__0()   在   Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。<> c__DisplayClass3_0`1.b__0()   在   Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作   动作)值不能为null。参数名称:connectionString

我的ConnectionString来自在Visual Studio中设置的环境变量。我不确定如何执行迁移。

我还需要做什么?

编辑:

Startup.cs中的DbContext

services.AddDbContext<DocumentStudioDbContext>(options => options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString)));

EDIT2:

enter image description here

设法从环境变量中取出变量

1 个答案:

答案 0 :(得分:0)

  <button mat-button color="primary">Primary</button>

//////在您的上下文中编写此代码

Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimeTypes = {"image/png", "image/jpg", "image/jpeg"};
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_GALLERY);