如何在 Razorpages 教程中处理添加迁移时出现空参数错误

Posted

技术标签:

【中文标题】如何在 Razorpages 教程中处理添加迁移时出现空参数错误【英文标题】:How to Handle null parameter Error On Add-Migrations in Razorpages tutorial 【发布时间】:2018-02-25 04:29:37 【问题描述】:

我全新安装了 VS 2016 15.3 和 Dot Net Core 2.0 SDK

我正在关注tutorial for Dot Net Core 2.0 Razorpages 并已达到“模型”步骤。在那里,我根据指令创建了单个实体和 dbcontext,并将连接字符串代码添加到教程中指定的位置。尽我所能,我的项目与教程完全一样。

当我进入 PM 并运行 Add-Migration Initial 时, 我得到一个“参数空异常:参数错误,表明它找不到配置字符串”。本教程没有要求Add-Migration 命令的任何参数,也没有提供有关此问题的任何指导。

如何继续本教程。?似乎没有办法联系到教程的作者。

我在 appsettings 中定义的连接字符串是:

"ConnectionStrings": 
  "**MovieContext**": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;MultipleActiveResultSets=true"

Startup.cs 有 DbContext 服务正在寻找该连接字符串:

services.AddDbContext<MovieContext>(options =>                
    options.UseSqlServer(Configuration.GetConnectionString("**MovieContext**")));

这是我尝试运行Add-Migration Initial时的输出:

PM> Add-Migration Initial
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\RRR\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
**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`1 sqlServerOptionsAction)
   at RazorPagesMovie.Startup.<ConfigureServices>b__4_0(DbContextOptionsBuilder options) in C:\Users\RRR\Source\Repos\RazorPagesMovie\RazorPagesMovie\Startup.cs:line 29
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass0_0`1.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)
   at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass5_0`1.<AddCoreServices>b__0(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.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.<>c__5`1.<AddCoreServices>b__5_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.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.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null.
Parameter name: connectionString

【问题讨论】:

【参考方案1】:

发现错误 - 阅读障碍和视力不佳: 错误在 appsettings.json 文件中 我错误地添加了连接字符串:

  
  "Logging": 
    "IncludeScopes": false,
    "LogLevel": 
      "Default": "Warning"
    ,
    "ConnectionStrings": 
      "MovieContext": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;MultipleActiveResultSets=true"
    
  

(使其成为 Logging 的一部分,而不是正确版本中的第一级条目:)


  "Logging": 
    "IncludeScopes": false,
    "LogLevel": 
      "Default": "Warning"
    
  ,
    "ConnectionStrings": 
      "MovieContext": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;MultipleActiveResultSets=true"
    

【讨论】:

以上是关于如何在 Razorpages 教程中处理添加迁移时出现空参数错误的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core 2.2 Razor 页面中的自定义路由

如何在 Razor Pages 路由的开头添加应用程序名称?

当迁移在另一个项目中使用 cli 时,我应该如何添加新的迁移?

Razor Pages,表单页面处理程序不使用 GET 方法

RazorPages Page Remote 在模型上不起作用

如何在 Ruby on Rails ActiveRecord 迁移中处理过长的索引名称?