A
A
Andrey2018-02-10 17:50:43
ASP.NET
Andrey, 2018-02-10 17:50:43

ASP.NET Core What's wrong with Add-Migration Initial?

Good day, dear experts.
I decided to start learning C # and ASP.NET Core and for informational purposes I began to do a lesson.
Here is a link to it:
https://docs.microsoft.com/ru-ru/aspnet/core/tuto...
All the previous steps are naturally completed.
And I'm stuck on Add-Migration Initial
When I enter this command, I get this error:

Type 'get-help NuGet' to see all available NuGet commands.

PM> Add-Migration Initial
Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\Андрей\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.<>c__DisplayClass4_0.<ConfigureServices>b__0(DbContextOptionsBuilder options) in C:\Users\Андрей\Documents\MEGAsync\CSharp\source\repos\RazorPagesMovie\RazorPagesMovie\Startup.cs:line 28
   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
PM>

Save!
I met a similar problem on the Internet, but I did not find a specific solution.
I downloaded the same lesson from github, it worked without problems.
What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2018-02-11
@andrey71

Swears but the fact that the connectionString line is not set in appsettings.json

{
 "Data": {
  	"ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=clothapp;Trusted_Connection=True;"
  }
}

W
Weldervan, 2021-04-16
@Weldervan

To isolate the problem, try a few simple steps:
1)
appsettings.json

{
"ConnectionStrings": {
    "MovieContext": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;Trusted_Connection=True;"
  }
}

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

If it doesn't help, try connecting without the extension method:
2)
services.AddDbContext<MovieContext>(options =>
            options.UseSqlServer(Configuration["ConnectionStrings:MovieContext"]));

If this time it gives an error, but of a different nature - for example "Object reference not set to an instance of an object",
then you have several constructs defined in the startup.cs class, remove all constructors except for the constructor from the IConfiguration interface.
If it doesn't help, then pass the connection string directly to:
services.AddDbContext<AppIdentityDbContext>(options =>
            options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=clothapp;Trusted_Connection=True;"));

The fact is that NullException may not necessarily refer to the connection string, or to the appsettings.json file, etc., an object was not created as a result of this null ..
see not because of what it happened, but why it happened
You use static methods and the constructor is never called, the object is not created, the
constructor is called, but with the wrong parameters (as an option, the implementation of the default constructor in the class, before the implementation of the constructor with parameters),
an error in the line name instead of MovieContext is written for example MoveContext ,
error in the file name instead of appsettings.json, for example, appsetings.json is written,
the error is in the appsettings.json file itself instead of "ConnectionStrings": {"MovieContext": .....,
it is written for example "ConnectionString": { "MovieContext": ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question