初始化字符串的格式不符合从服务索引0开始的规范。

时间:2018-09-27 20:36:20

标签: c# asp.net entity-framework hangfire hangfire-sql

我正在尝试通过其网站上的文档将hangfire集成到我的应用程序中,但是,在尝试运行该应用程序时出现此错误

System.ArgumentException: 'Format of the initialization string does not conform to specification starting at index 0.'

在我的Startup.cs文件的这一行代码中:

 services.AddHangfire(x => x.UseSqlServerStorage("HangfireDb"));

我首先使用实体​​框架代码添加了Db,这是文件的样子:

public partial class HangfireDbContext : DbContext
{
    public HangfireDbContext(DbContextOptions<HangfireDbContext> options) : base(options) { }
}

使用一个DBFactory调用存储在appsettings文件中的连接字符串。

public class HangfireDbContextFactory : IDesignTimeDbContextFactory<HangfireDbContext>
{
    public HangfireDbContext CreateDbContext(string[] args)
    {
        var basePath = AppContext.BaseDirectory;
        var environmentName = Environment.GetEnvironmentVariable(DalConstants.HostingEnvironment);

        return Create(basePath, environmentName);
    }

    private HangfireDbContext Create(string basePath, string environmentName)
    {
        IConfigurationRoot config = new ConfigurationBuilder()
        .SetBasePath(basePath)
        .AddJsonFile(DalConstants.AppSettingsName)
        .Build();

        string connectionString = config.GetConnectionString(DalConstants.HangfireDb);

        if (string.IsNullOrWhiteSpace(connectionString))
        {
            throw new InvalidOperationException($"Could not find a connection string named '{DalConstants.HangfireDb}'.");
        }

        var optionsBuilder = new DbContextOptionsBuilder<HangfireDbContext>();
        optionsBuilder.UseSqlServer(connectionString);

        return new HangfireDbContext(optionsBuilder.Options);
    }
}

appsettings.json

"ConnectionStrings": {

"HangfireDb": "Server=serverName;User Id=userName;password=userPassword;Database=HangfireDb;MultipleActiveResultSets=true"
 },

我已经运行了迁移,并且数据库已成功创建,并且那里没有问题。我在这里想念什么吗?谢谢。

0 个答案:

没有答案