Can I inject dependency into migration (using EF-Core code-first migrations)?

前端 未结 3 1108
感动是毒
感动是毒 2021-02-19 03:20

I tried to inject IConfiguration into the migration (in constructor), and got exception: \"No parameterless constructor defined for this object.\"

any worka

3条回答
  •  礼貌的吻别
    2021-02-19 04:03

    If it is just about your connection-string (is it?), you may want to check this answer, which basically suggests this code in your startup-project (not in your migrations-project):

    var myConnectionString = Configuration.GetConnectionString(myConnectionStringName);
    services.AddDbContext(options => options.UseSqlServer(
        myConnectionString ,
        x => x.MigrationsAssembly(myDbContextAssemblyName)));
    

提交回复
热议问题