0

I have a separate database for every client. I want to initialize EF Core context using dependency injection at function app startup. How can I achieve this?

I want to avoid initializing a connection for the same database on every function call when the function app is already running.

tehmas
  • 164
  • 1
  • 8
  • Pass the connection string in a constructor. See [here](https://stackoverflow.com/q/38878140/861716). – Gert Arnold May 03 '20 at 18:44
  • @GertArnold I have a single function app and a growling list of database per client i.e. each client has a separate database but the schema of every database is the same. The referenced approach seems suitable only if there is one database. – tehmas May 04 '20 at 06:54
  • Did you read the accepted answer? – Gert Arnold May 04 '20 at 07:25
  • @tehmas have you referred to https://markheath.net/post/ef-core-di-azure-functions? – Jim Xu May 04 '20 at 07:42
  • @JimXu I had referred to Jeff's article for injecting context of the database which has the tenant profiles of all the tenants e.g. the db name, server etc. of a particular tenant. When a function is triggered, say by a queue message, it contains the tenant name to specify whose message is this. I'm using the injected dependency to get the db credentials to establish a connection with the desired db. I want to improve the second step. Establishing a connection multiple times from a set of multiple dbs is an overhead. https://dev.to/azure/using-entity-framework-with-azure-functions-50aa – tehmas May 05 '20 at 04:58
  • @GertArnold As I mentioned the stated answer is not what I'm trying to achieve. I'm already using that approach to connect to the database having all tenant profiles. What I would like to improve is the step of actually connecting to a tenant's database. Please refer to the above reply (to JimXu comment). Kindly let me know if you have more questions. Thanks – tehmas May 05 '20 at 05:01
  • @tehmas I think you can create multiple db context to connect different database – Jim Xu May 05 '20 at 05:10
  • @JimXu That would have drawbacks: (1) New deployment would be needed every time a new tenant/client is added (2) It would not be efficient with large number of clients/tenant – tehmas May 05 '20 at 05:19
  • @tehmas There seems to be some misunderstanding between you and Jim. It's about one context *class* but many run-time *instances*. One for each request. That's the recommended way of working with EF. And that's why you can inject the connection string for each instance. – Gert Arnold May 05 '20 at 06:59

0 Answers0