Using a dynamic connection string with the Breeze EFContextProvider

时光怂恿深爱的人放手 提交于 2019-12-01 21:22:12

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;

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.

The CreateContext virtual method, mentioned by Ward, is now available in v 0.83.2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!