Silverlight实体框架动态连接字符串
我在一堆不同的服务器上有相同的数据模型。我想根据我的用户是谁以及他们在做什么动态地创建一个连接字符串 我的用户可以在多台服务器上拥有多个数据库。当我创建DomainService时,我需要一种干净的方法来构建连接字符串 我看到DomainService有一个名为CreateObjectContext()的覆盖(继承自LinqToEntitiesDomainService),它允许我设置任何想要的连接字符串,然后返回新实体,这样就很好了。问题是,CreateObjectContext()在构造函数之后被调用,因此我无法通过invoke方法设置字符串。此外,我还尝试在DomainService上创建一个新的参数化构造函数,但它从未被复制到客户端的DomainContext中 如果我能够提取连接字符串,CreateObjectContext()将非常有用,但是由于我必须使用来自客户端的数据来确定要连接哪个数据库,这显然不起作用 我想得越多,我就越觉得自定义构造函数正是我所需要的——只是不知道如何完成它Silverlight实体框架动态连接字符串,silverlight,entity-framework,connection-string,Silverlight,Entity Framework,Connection String,我在一堆不同的服务器上有相同的数据模型。我想根据我的用户是谁以及他们在做什么动态地创建一个连接字符串 我的用户可以在多台服务器上拥有多个数据库。当我创建DomainService时,我需要一种干净的方法来构建连接字符串 我看到DomainService有一个名为CreateObjectContext()的覆盖(继承自LinqToEntitiesDomainService),它允许我设置任何想要的连接字符串,然后返回新实体,这样就很好了。问题是,CreateObjectContext()在构造函数
我遗漏了什么?我找到了解决办法。对于感兴趣的人,这里是: 这感觉有点像黑客,但这是我能想出的唯一解决办法。感谢来自forums.silverlight.net的Sally Xu的想法 因为我的每个用户都可以在多台服务器上拥有多个数据库,所以我需要在第一次使用DomainService之前找到一种创建ConnectionString的方法。每次用户从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);
}
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);
}
cookieName是SelectedProjectId
,cookieValue是我的UI中当前选定的项目
然后我像往常一样创建了一个新的DomainService,但是我重写了CreateObjectContext()
。此方法在您第一次引用DomainService对象时被调用。我的覆盖如下所示:
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);
}
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);
}
同样,这也有点不成熟,但这是我能找到的唯一一种根据需要动态创建连接字符串的方法。我希望这能帮助其他人…我找到了一个解决方案。对于感兴趣的人,这里是: 这感觉有点像黑客,但这是我能想出的唯一解决办法。感谢来自forums.silverlight.net的Sally Xu的想法 因为我的每个用户都可以在多台服务器上拥有多个数据库,所以我需要在第一次使用DomainService之前找到一种创建ConnectionString的方法。每次用户从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);
}
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);
}
cookieName是SelectedProjectId
,cookieValue是我的UI中当前选定的项目
然后我像往常一样创建了一个新的DomainService,但是我重写了CreateObjectContext()
。此方法在您第一次引用DomainService对象时被调用。我的覆盖如下所示:
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);
}
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);
}
同样,这也有点不成熟,但这是我能找到的唯一一种根据需要动态创建连接字符串的方法。我希望这能帮助其他人…WCF数据服务怎么样?WCF数据服务怎么样?