MJ X
MJ X

Reputation: 9054

Entity Framework Core console app - database connection generates ArgumentNullException

I have created a console app and I need to use Entity Framework Core for some database transactions, however it's not working when I run my app.

This is my DbContext class:

public partial class MyDbContext : DbContext
{
    public MyDbContext ()
    {
    }

    public MyDbContext (DbContextOptions<MyDbContext > options)
        : base(options)
    {
    }

    public virtual DbSet<MyUsers> MyUser{ get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(ConfigurationManager.AppSettings["DbConnectionString"]);
        }
    }
}

My app.config:

<configuration>
   <appSettings>
       <add key="DbConnectionString" value= "myconnectionstring" />
  </appSettings>
</configuration>

This is how I am calling it:

  MyDbContext _context = new MyDbContext();

  var user = _context.MyUsers.FirstOrDefault(u => u.Email == email);

When I run add-migration in console, I get the following error:

Build started...
Build succeeded.

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')

at Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(String value, String parameterName)
at Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder, String connectionString, Action1 sqlServerOptionsAction) at ReGeneratePassword.EntityFrameWorkFiles.MyDbContext .OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Dev\test\ReGeneratePassword\EntityFrameWorkFiles\MyDbContext.cs:line 40 at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure1 accessor) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Upvotes: 0

Views: 1497

Answers (1)

DoDoMeista
DoDoMeista

Reputation: 1

How does your appsettings.json look? Have you added your connection string there? Is it the right name? Becuase it says the parameter "connectionString" is null. So maybe you either have the wrong name for it or you havent added your connection string like this in appsettings.json which is where your saying its coming from by this ->

optionsBuilder.UseSqlServer(ConfigurationManager.AppSettings["DbConnectionString"]);

Your appsettings.json should look something like this

{
  "ConnectionStrings": {
    "Connection": "server=(localdb)\\MSSQLLocalDB;database=TimeReport.API;Trusted_Connection=True;"
  },

Upvotes: 0

Related Questions