NInject and Migration using EF Core in Console app

时间:2019-04-23 15:20:07

标签: .net-core ef-core-2.0

I am using NInject container for Dependency Injection and Entity Framework Core as ORM. The setup is as follows:

DB Context Class

    public TarantoContext()
        {

        }
public TarantoContext(DbContextOptions<TarantoContext> options)
        : base(options)
    {
    }

    public virtual DbSet<FileData> FileData { get; set; }
    public virtual DbSet<FileExport> FileExport { get; set; }
    public virtual DbSet<FileStatus> FileStatus { get; set; }
    public virtual DbSet<FileType> FileType { get; set; }
    public static string ConnectionString { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(ConnectionString);
        }
    }

Program.cs

    TarantoContext.ConnectionString = configurationManager.DatabaseConnection;

I am reading the configuration from json and passing it to the DataAccess layer (to the context class). I have a few tables in my database and taking database first approach I created the necessary data models and completed the dbcontext class code. I am able to fetch the data without any problems. Now I want to add-migration (I may have more changes to existing tables and may create new tables) and ran the following in package manager console:

Add-Migration InitialCreate

which resulted in the following error because the connectionstring property is null

enter image description here

I can fix this by hardcoding the connectionstring in the OnConfiguring method which I have tried without any problems:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            // The connection string needs to exist in the migration project for the purpose of migrations. Comment it in production.
            optionsBuilder.UseSqlServer(
                "Data Source=temp;Initial Catalog=Demo;Persist Security Info=True;User ID=temp;Password=temp;MultipleActiveResultSets=True;");
            //optionsBuilder.UseSqlServer(ConnectionString);
        }
    }

but I do not think this is the correct way of doing migrations. Moreover I am not sure how to handle the migrations in productions if I am unable to set the connectionstring in Program.cs or outside of the dbcontext class. What I am interested in is learning any design pattern which other developers are using to handle this situation. Any advise is greatly appreciated.

1 个答案:

答案 0 :(得分:0)

在ASP.NET Core中,您可以在ConfigureServices方法中设置服务时加载连接字符串。使用以下代码:

 services.AddDbContext<ApplicationDatabaseContext>(options =>
     options.UseSqlServer(
         Configuration.GetConnectionString("DefaultConnection"),
         b => b.MigrationsAssembly("....")));

现在,GetConnectionString调用将从您的配置文件(即appsettings.json)中检索连接字符串。要为不同的环境处理不同的连接字符串,可以使用appsettings.<Environment>.json之类的appsettings.Production.json文件覆盖默认的应用程序设置。

此处的更多信息:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2

请查阅相关的MSDN文档页面以获取这些信息,因为它们包含很多信息。