Mark Hollas
Mark Hollas

Reputation: 1137

EF Core 2 w/ UsingSqlServer DB not created

I am getting into Core 2 and in my initial dev I had been using the in memory db option for Entity Framework and things ran smooth. Getting ready to deploy, I switched over to UseSqlServer option and tried to add migrations to create my db, running

Add-Migration InitialCreate 

errors out, stating the connection string is null.

    PM> Add-Migration InitialCreate
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 Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer[TContext](DbContextOptionsBuilder`1 optionsBuilder, String connectionString, Action`1 sqlServerOptionsAction)
   at SmartMiner.API.DesignTimeDbContextFactory.CreateDbContext(String[] args) in C:\code\SmartMiner\SmartMiner copy\SmartMiner.API\DesignTimeDbContextFactory.cs:line 19
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_0.<FindContextTypes>b__3()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   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

When launching in debug I can see that when StartUp.cs calls

services.AddDbContextPool<ApplicationContext>(options => {             options.UseSqlServer(Configuration.GetConnectionString("DebugContext"));
            });

using ImmediateWindow I can confirm that Configuration.GetConnectionString("DebugContext")

does return the stated connection string

the connection string being

    {
  "ConnectionStrings": {
    "ProdContext": "Data Source=tcp:azuredb.database.windows.net,1433;Initial Catalog=MYDB;User [email protected];Password=xxxx;",
    "DebugContext": "Server=(localdb)\\mssqllocaldb;Database=DebugDb;Trusted_Connection=True;"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  }
}

Nor when I run the project and do an operation such as registering a new user, which is supposed (or used to) trigger the full creation of the db if it does not exist.

I have had no issues in the days of dotnet 4.5 and web.config... but am feeling a little lost and dumbfounded here not even being able to create my db...

Upvotes: 1

Views: 452

Answers (2)

Mark Hollas
Mark Hollas

Reputation: 1137

Entity Framework Migration seems to be looking for a specifically named connection string. i.e "DefaultConnection". I added the following line to my appsettings.json and it works.

"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DebugDb;Trusted_Connection=True;"

Upvotes: 0

Zeeshan
Zeeshan

Reputation: 3024

You are one step away. Add-Migration InitialCreate doesn't actually creates database. It only creates migrations. After migrations has been created, you need to apply the created migrations logic, in order to have the physical effect of it. Run following command in order to apply the created migrations.

Update-Database

This will apply the pending migrations, hence generating the database/tables etc. Please refer to Migrations - EF Core with ASP.NET Core MVC docs for detailed understanding

Upvotes: 1

Related Questions