2

I have Entity Framework DbContext like this

class MyClassDBContext:DBContext,IMyClassDBContext
{
     public MyClassDBContext() : base(str, true)
     {
          this.Database.Connection.ConnectionString = str;
     }
}

This works well. I also use Unity framework to add dependency injection of this class to other layers i.e business layer using web.config like this:

<register type="IMyClassDBContext" mapTo="MyClassDBContext">
    <lifetime type="perRequestLifetimeManager" />
</register>

Now, I want to find a way to inject the connection str at runtime into the DbContext using Unity. I can get the connection string from Azure KeyVault in startup, but not really sure how I can inject in the Entity Framework constructor.

This is a Web API project with Entity Framework 6

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mandar Jogalekar
  • 3,199
  • 7
  • 44
  • 85
  • Refer: https://stackoverflow.com/questions/2797047/how-do-i-correctly-use-unity-to-pass-a-connectionstring-to-my-repository-classes – nithinmohantk Jun 15 '18 at 12:21
  • I did this a while back by creating a SystemConfiguration interface and class and injecting that class into the data controllers via constructor injection. You could configure it in a way that your configuration class exposed a method to get the connection string that way it does not matter to your db controller classes where or how you obtain the connection string. – Ross Bush Jun 15 '18 at 13:32

0 Answers0