Silverlight实体框架动态连接字符串 [英] Silverlight Entity Framework Dynamic Connection String
问题描述
我在一堆不同的服务器上有相同的数据模型。我想根据我的用户身份和他们的工作动态创建连接字符串。
I have the same data model on a bunch of different servers. I want to dynamically create a connection string based on who my user is and what they are doing.
我的用户可以在多个服务器上拥有多个数据库。创建我的DomainService时,我需要一种干净的方法来构建一个connectoin字符串。
My users can have multiple databases on multiple servers. I need a clean way to build a connectoin string when I create my DomainService.
我看到DomainService有一个重写(从LinqToEntitiesDomainService继承),称为CreateObjectContext()请允许我设置所需的任何连接字符串,然后返回新实体,生活会很美好。问题是,CreateObjectContext()在构造函数之后被调用,所以我无法通过invoke方法设置字符串。另外,我尝试在DomainService上创建一个新的参数化构造函数,但永远不会将其复制到客户端上的DomainContext。
I see that the DomainService has an override (inherited from LinqToEntitiesDomainService) called CreateObjectContext() that would allow me to set any connection string I want, then return the new entity and life is good. The problem is, the CreateObjectContext() gets called after the constructor, so I can't set a string via an invoke method. Also, I've tried to create a new parameterized constructor on the DomainService, but it never gets copied to the DomainContext on the client.
CreateObjectContext()会很好用如果我能够拉出我的连接字符串,但是由于必须使用客户端中的数据来确定要连接的数据库,这显然将不起作用。
The CreateObjectContext() would work great if I was able to pull my connection string, but since I have to use data from the client to figure out which DB to connect, this obviously won't work.
我想得越多,我就越觉得自定义构造函数正是我所需要的-只是不知道该怎么做。
The more I think about it, the more I feel a custom constructor is exactly what I need - just can't figure out how to get that done.
我是什么
推荐答案
我找到了解决方案。对于那些感兴趣的人,这里是:
I found a solution. For those that are interested, here it is:
这听起来有点像黑客,但这是我唯一能想到的解决方案。多亏了Sally Xu在forums.silverlight.net上提出的想法。
This feels a bit like a hack, but it's the only solution I could come up with. Thanks to Sally Xu over at forums.silverlight.net for the ideas.
由于我的每个用户都可以在多个服务器上拥有多个数据库,因此我需要找到一种方法来在第一次使用DomainService之前创建ConnectionString。每当用户从用户界面中选择一个新项目时,我都会设置如下Cookie:
Since each of my users can have mulitiple databases on multiple servers, I needed to find a way to create the ConnectionString before the DomainService was used the first time. Each time the user selects a new project from the UI, I set a cookie like this:
private void SetCookie(string cookieName, string cookieValue)
{
DateTime expireDate = DateTime.Now + TimeSpan.FromDays(1);
string newCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate.ToString("R");
HtmlPage.Document.SetProperty("cookie", newCookie);
}
cookieName为 SelectedProjectId
> cookieValue是UI中当前选择的项目。
The cookieName is SelectedProjectId
and the cookieValue is the current selected project in my UI.
然后我像往常一样创建了一个新的DomainService,但是我覆盖了 CreateObjectContext()
。首次引用DomainService对象时,将调用此方法。我的替代看起来像这样:
Then I create a new DomainService as normal, but I override CreateObjectContext()
. This method gets called the very first time you reference your DomainService object. My override looks like this:
protected override ProjectEntities CreateObjectContext()
{
long projectId = -1;
StringBuilder connection;
if (System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"] != null)
{
projectId = Convert.ToInt64(System.Web.HttpContext.Current.Request.Cookies["SelectedProjectId"].Value);
}
else throw new Exception("Selected Project ID Exception"); // temporary
// Verify this user has access to the DB just in case it's spoofed
// Lookup project ID in my database to get the database name and server name
// Load template connection string found in web.config
// Replace the template holders for SERVER_NAME and DATABASE_NAME with above lookup values
return new ProjectEntities(MyDynamicConnectionString);
}
再说一次,这有点黑,但这是我唯一的方法查找动态创建满足我需求的连接字符串。我希望这可以帮助其他人...
Again, this is a bit hackish, but it was the only way I could find to dynamically create a connection string for my needs. I hope this helps someone else...
这篇关于Silverlight实体框架动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!