简体   繁体   English

实体框架核心 - “更新数据库”不起作用

[英]Entity Framework Core - 'Update-Database' not working

In my solution I have several .NET Core 2.0 projects:在我的解决方案中,我有几个 .NET Core 2.0 项目:

  • App.DataAccess应用程序数据访问
  • App.DataAccess.Migrations App.DataAccess.Migrations
  • App.WebApi应用程序WebApi

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.

相关问题 Entity Framework Core 不在更新数据库上创建数据库 - Entity Framework Core not creating database on update-database 实体框架核心更新-数据库特定迁移 - Entity framework Core Update-database specific migration 在 Entity Framework Core 中使用 Update-Database 添加新列 - Adding new column using Update-Database in Entity Framework Core 如何在.NET Core中撤消Entity Framework Update-Database - How to undo Entity Framework Update-Database in .NET Core Update-Database 命令在 ASP.Net Core / Entity Framework Core 中不起作用,因为数据库中的对象已经存在 - Update-Database command is not working in ASP.Net Core / Entity Framework Core because object in database already exists 带有-ConnectionString的实体框架更新数据库 - Entity Framework Update-Database with -ConnectionString 实体框架6中“更新数据库”之后发生错误 - Error after 'update-database' in Entity Framework 6 实体框架核心更新-数据库特定的迁移没有权限错误 - Entity Framework Core Update-database specific migration gives no permission error 实体框架核心,更新数据库不考虑属性 - Entity framework core, update-database don't take account of attributes 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM