问题
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.
回答1:
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;
回答2:
I downloaded the source and added a constructor to the EFContextProvider which accepts an instance of T to be able to use an existing ObjectContext/DbContext which works like a charm.
Marcel figured it out by himself and answered his own question on our forum.
回答3:
The CreateContext virtual method, mentioned by Ward, is now available in v 0.83.2
来源:https://stackoverflow.com/questions/13616832/using-a-dynamic-connection-string-with-the-breeze-efcontextprovider