Unable to implement IDbContextFactory for use with Entity Framework data migrations

折月煮酒 提交于 2019-12-12 04:38:11

问题


Tried to implement IDbContextFactory as follows. And got the following error at lines return Create(options.ContentRootPath, options.EnvironmentName); and var optionsBuilder = new DbContextOptionsBuilder<MyProjContext>(); when running following PM command

PM> add-migration MyMigration -context MyProjContext

Error

An error occurred while calling method 'ConfigureServices' on startup class 'Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Could not find 'UserSecretsIdAttribute' on assembly 'ef, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. System.InvalidOperationException: Could not find a connection string named 'MyProjContext'. ....

Note Both MyProjCnotext.cs and MyProjContextFactory.cs files are are in MyProj\Models folder

MyProjContextFactory class

public class MyProjContextFactory : IDbContextFactory<MyProjContext>
{
    public MyProjContext Create()
    {
        var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment");
        return Create(Directory.GetCurrentDirectory(), environmentName);
    }

    public MyProjContext Create(DbContextFactoryOptions options)
    {
        return Create(options.ContentRootPath, options.EnvironmentName);
    }

    public MyProjContext Create(string basePath, string environmentName)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(basePath)
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{environmentName}.json", true)
            .AddEnvironmentVariables();

        var Configuration = builder.Build();

        var connectionName = nameof(MyProjContext);
        var connectionString = Configuration.GetConnectionString(connectionName);
        if (String.IsNullOrWhiteSpace(connectionString) == true)
            throw new InvalidOperationException($"Could not find a connection string named '{connectionName}'.");

        // init SQL server
        var optionsBuilder = new DbContextOptionsBuilder<MyProjContext>();
        optionsBuilder.UseSqlServer(connectionString);

        return new MyProjContext(optionsBuilder.Options);
    }
}

MyProjContext.cs

namespace MyProj.Models
{
public class MyProjContext : DbContext
{
    public MyProjContext(DbContextOptions<MyProjContext> options) : base(options)
    {

    }
    public DbSet<Table1> Table1 { get; set; }
    public DbSet<Table2> Table2 { get; set; }
    ...
}
}

回答1:


You threw an exception: throw new InvalidOperationException($"Could not find a connection string named '{connectionName}'.");

Add in appsettings.json and appsettings.Development.json:

{
    ...
    "ConnectionStrings": {
        "MyProjContext": "..."
    }
}


来源:https://stackoverflow.com/questions/45654180/unable-to-implement-idbcontextfactory-for-use-with-entity-framework-data-migrati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!