Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a dynamic connection string with the Breeze EFContextProvider

At the moment i have an application (web/silverlight) where the connectionstring for my ObjectContext is dynamic. It is based on how a user logs in because each of my customers have their own database. ie.. username@domain. I'm trying to find a way to use the EFContextProvider which would be by either passing the ObjectContext through the constructor, or by overriding the GetConnectionString, which sadly both aren't supported.

Is there a way to accomplish this, or can i download the source for the EFContextProvider somewhere so i can implement it myself ?

Thanks in advance.

This question was posted by Marcel on our IdeaBlade forums. I am reposting the question and answer here since I think it will be useful to the Breeze Stack Overflow community.

like image 804
Ting Avatar asked Apr 28 '26 20:04

Ting


1 Answers

You shouldn't have to download the source and modify it for such a simple thing. And now you won't have to.

We've pushed to GitHub a simple update to EFContextProvider. This change will appear in the next Breeze Runtime version (> 0.81.2).

Where EFContextProvider used to create the 'T' (your ObjectContext/DbContext) as follows:

_context = new T();

It now calls upon a virtual method, T CreateContext() instead, whose default implementation is:

protected virtual T CreateContext() {
    return new T();
}

Override and replace that in your EFContextProvider subclass and you will be able to make your context of type 'T' just the way you like it.

N.B.: The base EFContextProvider will still do a little post-creation configuration to make sure it behaves as we expect; we don't want the context doing any lazy loading or creating proxies.

So if 'T' is an ObjectContext, the provider will do this:

objCtx.ContextOptions.LazyLoadingEnabled = false;

and if 'T' is a DbContext it will do this:

dbCtx.Configuration.ProxyCreationEnabled = false;
dbCtx.Configuration.LazyLoadingEnabled = false;
like image 115
Ward Avatar answered Apr 30 '26 15:04

Ward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!