简体   繁体   English

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. 我想根据用户是谁以及他们在做什么动态创建连接字符串。

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时,我需要一种干净的方法来构建连接字符串。

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. 我看到DomainService有一个重写(继承自LinqToEntitiesDomainService),称为CreateObjectContext(),它将允许我设置所需的任何连接字符串,然后返回新实体,并且生活很美好。 The problem is, the CreateObjectContext() gets called after the constructor, so I can't set a string via an invoke method. 问题是,CreateObjectContext()在构造函数之后被调用,所以我无法通过invoke方法设置字符串。 Also, I've tried to create a new parameterized constructor on the DomainService, but it never gets copied to the DomainContext on the client. 另外,我尝试在DomainService上创建一个新的参数化构造函数,但从未将其复制到客户端上的DomainContext中。

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. 如果我能够拉出我的连接字符串,CreateObjectContext()会很好用,但是由于我必须使用来自客户端的数据来确定要连接的数据库,所以这显然行不通。

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. 我考虑的越多,我就越觉得自定义构造函数正是我所需要的-只是不知道如何完成该工作。

What am I missing? 我想念什么?

I found a solution. 我找到了解决方案。 For those that are interested, here it is: 对于那些感兴趣的人,这里是:

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. 感谢Sally Xu在forums.silverlight.net上获得的想法。

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. 由于我的每个用户都可以在多个服务器上拥有多个数据库,因此我需要在首次使用DomainService之前找到一种创建ConnectionString的方法。 Each time the user selects a new project from the UI, I set a cookie like this: 每次用户从UI中选择一个新项目时,我都会设置如下cookie:

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);
}

The cookieName is SelectedProjectId and the cookieValue is the current selected project in my UI. cookieName是SelectedProjectId ,而cookieValue是UI中当前选择的项目。

Then I create a new DomainService as normal, but I override CreateObjectContext() . 然后,像往常一样创建一个新的DomainService,但是我覆盖了CreateObjectContext() This method gets called the very first time you reference your DomainService object. 首次引用DomainService对象时,将调用此方法。 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... 我希望这可以帮助其他人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM