【问题标题】:ASP.NET Core RC2 Seed Data using Services使用服务的 ASP.NET Core RC2 种子数据
【发布时间】:2016-09-18 18:57:57
【问题描述】:

我一直在使用 ASP.NET 5 RC1,其中我有一个应用程序,我在其中使用服务将管理凭据播种到数据库中,如下所示:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<AdministratorUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        services.AddTransient<AdministratorSeedData>();
    }

这里是连接字符串子对象:

 "ConnectionString": "Server=.;Database=kjanshair;Trusted_Connection=True;"

我用来播种数据的服务正在使用管理播种数据播种。一切正常,但是当我将其移植到 RC2 时,启动时发生以下更改:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options => 
        options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        services.AddTransient<AdministratorSeedData>();
    }

连接字符串为:

"ConnectionString": "Server=.;Database=_CHANGE_ME;Trusted_Connection=True;"

我在 Configure() 方法中遇到异常

public async void Configure(IApplicationBuilder app, AdministratorSeedData seeder)
    {
        app.UseIdentity();

        app.UseMvcWithDefaultRoute();

        await seeder.EnsureSeedData(); //Here exception occurs
    }

说:

Message = "Cannot open database \"_CHANGE_ME\" requested by the login. The login failed.\r\nLogin failed for user 'DESKTOP-55E4D9H\\Janshair Khan'."

为什么会出现此错误?我已经查看了所有替代方案,但无法解决。

【问题讨论】:

  • 查看 Julie lerman“数据农场”的最新帖子。可能会帮助你

标签: sql-server asp.net-core asp.net-identity entity-framework-core


【解决方案1】:

我认为你可以通过这样的改变来解决它:

services.AddEntityFrameworkSqlServer()
    .AddDbContext<ApplicationDbContext>(serviceProvider, options => 
      options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
    .UseInternalServiceProvider(serviceProvider)
    );

还要确保你的 DbContext 有这样的构造函数:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{

}

我遇到了同样的问题,这对我有用

【讨论】:

  • 方法AddSqlServer()与RC1兼容。官方 ASP.NET Core 无需此方法即可工作。但这是如何工作的呢?
  • 对不起,我的解决方案有误,我刚刚修复了它
  • 您应该显示如何添加 appsettings.json 或其他配置源的代码,它似乎没有从正确的位置获取您的连接字符串。 rc2 在配置中需要进行一些更改,如何设置基本路径
  • 我遵循默认模板配置方法,我将构造函数中的基本路径设置为基本路径,与模板显示为public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
  • 在他们使用的最新模板中 Configuration.GetConnectionString("DefaultConnection") 可能尝试像新项目一样更改您的 appsettings,这就是我现在所做的,我仔细观察
【解决方案2】:

我已经发现了问题,现在终于解决了。问题在于 RC2 身份生态系统的变化。我通过在终端中输入以下内容添加了迁移:

dotnet ef migrations add "migration message"

,更新数据库

dotnet ef database update

现在我可以在我的数据库中植入数据了。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    相关资源
    最近更新 更多