[英]Entity Framework Core - 'Update-Database' not working
In my solution I have several .NET Core 2.0 projects:在我的解决方案中,我有几个 .NET Core 2.0 项目:
In App.DataAccess.Migrations
(which I want to use as the migrations assembly) I have an AppDbContextFactory
class:在App.DataAccess.Migrations
(我想用作迁移程序集)中,我有一个AppDbContextFactory
类:
namespace App.DataAccess.Migrations
{
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;
public sealed class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContextFactory()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
}
public IConfiguration Configuration { get; }
public AppDbContext CreateDbContext(string[] args)
{
DbContextOptionsBuilder<AppDbContext> builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseSqlServer(
connectionString: Configuration.GetConnectionString("DefaultConnection"),
sqlServerOptionsAction: sqlOptions => sqlOptions.MigrationsAssembly(GetType().Namespace));
return new AppDbContext(builder.Options);
}
}
}
In the same project, I also have an appsettings.json
file:在同一个项目中,我还有一个appsettings.json
文件:
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=MyApp;User ID=sa;Password=SomethingSecret;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
In my App.WebApi
project, I have the following in my Startup
class:在我的App.WebApi
项目中,我的Startup
类中有以下内容:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(ConfigureDatabaseOptions);
services.AddMvc();
}
private void ConfigureDatabaseOptions(DbContextOptionsBuilder builder)
{
builder.UseSqlServer("DefaultConnection", ConfigureSqlServerDatabaseOptions);
}
private void ConfigureSqlServerDatabaseOptions(SqlServerDbContextOptionsBuilder builder)
{
builder.MigrationsAssembly(typeof(AppDbContextFactory).Namespace);
}
In the same project, I've also added the connection string to appsettings.json
.在同一个项目中,我还将连接字符串添加到appsettings.json
。
Add-Migration works.添加迁移有效。
Update-Database throws this error:更新数据库引发此错误:
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. System.ArgumentException: 初始化字符串的格式不符合从索引 0 开始的规范。
Why?为什么?
Solved it.解决了。 I'm passing "DefaultConnection" as the connection string instead of the connection string name :我将“DefaultConnection”作为连接字符串而不是连接字符串名称传递:
private void ConfigureDatabaseOptions(DbContextOptionsBuilder builder)
{
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), ConfigureSqlServerDatabaseOptions);
}
The way I could solve the problem was in the following way, in order to understand the solution I placed the name of a fictitious connectionString.我解决问题的方法如下,为了理解解决方案,我放置了一个虚构的connectionString的名称。 it´s solution它的解决方案
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString"))); public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionString"))); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddMvc().AddJsonOptions(configureJson); services.AddMvc().AddJsonOptions(configureJson); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.