无法实现IDbContextFactory用于实体框架数据迁移 [英] Unable to implement IDbContextFactory for use with Entity Framework data migrations

查看:344
本文介绍了无法实现IDbContextFactory用于实体框架数据迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图实现 IDbContextFactory 如下。并在行上得到以下错误,返回Create(options.ContentRootPath,options.EnvironmentName); var optionsBuilder = new DbContextOptionsBuilder< MyProjContext>(); 在运行以下 PM 命令

  PM> ;添加迁移MyMigration -context MyProjContext 

错误


在启动类启动中调用方法ConfigureServices时出错。考虑在设计时使用IDbContextFactory来覆盖DbContext的初始化。错误:在程序集'ef,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'上找不到UserSecretsIdAttribute。
System.InvalidOperationException:找不到名为MyProjContext的连接字符串。 ....


注意 MyProjCnotext.cs MyProjContextFactory.cs 文件位于 MyProj\Models 文件夹



MyProjContextFactory类

  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($无法找到名为{connectionName}的连接字符串。

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

返回新的MyProjContext(optionsBuilder.Options);
}
}

MyProjContext.cs / p>

 命名空间MyProj.Models 
{
public class MyProjContext:DbContext
{
public MyProjContext(DbContextOptions< MyProjContext>选项):base(options)
{

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


解决方案

你抛出一个异常: throw new InvalidOperationException($找不到一个名为{connectionName}的连接字符串);



添加到appsettings.json和appsettings.Development.json:

  {
...
ConnectionStrings:{
MyProjContext:...
}
}
/ pre>

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; }
    ...
}
}

解决方案

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": "..."
    }
}

这篇关于无法实现IDbContextFactory用于实体框架数据迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆