在Breeze EFContextProvider中使用动态连接字符串(Using a dynamic connection string with the Breeze EFContextProvi

编程入门 行业动态 更新时间:2024-10-19 03:27:39
在Breeze EFContextProvider中使用动态连接字符串(Using a dynamic connection string with the Breeze EFContextProvider)

目前我有一个应用程序(web / silverlight),其中我的ObjectContext的连接字符串是动态的。 它基于用户如何登录,因为我的每个客户都有自己的数据库。 即..用户名@域。 我试图找到一种方法来使用EFContextProvider,它可以通过构造函数传递ObjectContext,也可以通过覆盖GetConnectionString,这两种方法都不被支持。

有没有办法做到这一点,或者我可以下载EFContextProvider的源代码,以便我可以自己实现它?

提前致谢。

这个问题是由Marcel在我们的IdeaBlade论坛发布的。 因为我认为这对Breeze Stack Overflow社区有用,所以我在此重新发布问题和答案。

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.

最满意答案

您不必下载源代码并对其进行修改就可以实现这么简单的功能。 现在你不需要

我们已经推送给GitHub一个简单的更新到EFContextProvider 。 此更改将显示在下一个Breeze Runtime版本(> 0.81.2)中。

EFContextProvider用于创建'T'(您的ObjectContext / DbContext),如下所示:

_context = new T();

现在它调用一个虚方法T CreateContext() ,它的默认实现是:

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

覆盖并替换您的EFContextProvider子类中的内容,您将能够按照您喜欢的方式创建“T”类型的上下文。

注意:基础EFContextProvider仍然会进行一些后期创建配置 ,以确保它的行为符合我们的预期; 我们不希望上下文进行任何延迟加载或创建代理。

所以如果'T'是一个ObjectContext ,提供者将会这样做:

objCtx.ContextOptions.LazyLoadingEnabled = false;

如果'T'是一个DbContext它会这样做:

dbCtx.Configuration.ProxyCreationEnabled = false; dbCtx.Configuration.LazyLoadingEnabled = false;

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;

更多推荐

本文发布于:2023-07-26 00:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   动态   EFContextProvider   Breeze   connection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!