EF Core 2 w / UsingSqlServer DB未创建

时间:2018-01-17 09:40:36

标签: c# entity-framework asp.net-core

我正在进入Core 2,在我的初始开发中,我一直在使用内存db选项来实现Entity Framework,并且运行顺利。 准备部署,我切换到UseSqlServer选项并尝试添加迁移以创建我的数据库,运行

Add-Migration InitialCreate 

错误输出,说明连接字符串为空。

    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

在调试中启动时,我可以看到StartUp.cs调用

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

使用ImmediateWindow我可以确认Configuration.GetConnectionString("DebugContext")

会返回指定的连接字符串

连接字符串

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

当我运行项目并执行诸如注册新用户之类的操作时,也就是说,如果它不存在,则假定(或习惯)触发完全创建数据库。

在dotnet 4.5和web.config的时代,我没有遇到任何问题......但我感到有点迷茫,甚至没有能够创建我的数据库...... ...

2 个答案:

答案 0 :(得分:1)

你离我只有一步之遥。 mode: 'history', 实际上并不创建数据库。它只会创建迁移。创建迁移后,您需要应用创建的迁移逻辑,以获得其物理效果。运行以下命令以应用创建的迁移。

  

<强>更新的数据库的

这将应用挂起的迁移,从而生成数据库/表等。 有关详细说明,请参阅Migrations - EF Core with ASP.NET Core MVC文档

答案 1 :(得分:0)

实体框架迁移似乎正在寻找一个专门命名的连接字符串。即&#34; DefaultConnection&#34;。 我将以下行添加到我的appsettings.json中,它可以正常工作。

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