.Net Core Migration System.ArgumentNullException:值不能为空
Posted
技术标签:
【中文标题】.Net Core Migration System.ArgumentNullException:值不能为空【英文标题】:.Net Core Migration System.ArgumentNullException: Value cannot be null 【发布时间】:2019-11-01 17:18:35 【问题描述】:这是我的 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:值不能为空。参数名称: 连接字符串在 Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值, 字符串参数名称)在 Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder,字符串 connectionString,Action
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, Action2 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.CallSiteVisitor2.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(类型 serviceType, ServiceProviderEngineScope serviceProviderEngineScope) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型)在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供者,类型 serviceType) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider 提供者)在 Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.c__101.<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.CallSiteVisitor2.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(类型 serviceType, ServiceProviderEngineScope serviceProviderEngineScope) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型)在 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(字符串 名称)在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(字符串 上下文类型)在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串 名称,字符串 outputDir,字符串 contextType)在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串 名称,字符串 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(操作 动作)值不能为空。参数名称:连接字符串
我的 ConnectionString 来自 Visual Studio 中设置的环境变量。我不确定如何执行迁移。
我还需要做什么?
编辑:
Startup.cs 中的 DbContext
services.AddDbContext<DocumentStudioDbContext>(options => options.UseSqlServer(Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString)));
编辑2:
设法从环境变量中取出变量
【问题讨论】:
在 Statup 类中显示您的DbContext
注册,并在 appsettings.json
文件中显示连接字符串。
我的连接字符串存储在环境变量中。我不在项目中使用 appsettings.json。
好的!然后展示在DbContext
注册期间如何在 Startup 类中获得ConnectionSring
。
Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString) 是我获取连接字符串的方式。我尝试将它设置为一个变量,它可以工作。
Environment.GetEnvironmentVariable(DSCASGlobals.DS_ConnectionString)
这条线的ConnectionString
为空。请正确检查!将 ConnectionString 存储在环境变量而不是 appsettings.json 文件中的原因是什么?
【参考方案1】:
<configuration>
<connectionStrings>
<add name="DocumentStudioDb"
connectionString="Server=(localdb)\mssqllocaldb;Database=DocumentStudioDb;Trusted_Connection=True;" />
</connectionStrings>
</configuration>
/////在你的上下文中编写这段代码
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DocumentStudioDb"] .ConnectionString);
【讨论】:
以上是关于.Net Core Migration System.ArgumentNullException:值不能为空的主要内容,如果未能解决你的问题,请参考以下文章
.NET Core 5.0 EF Migration 添加新的外部列
ASP.NET Core EF Add-Migration 命令不起作用