LinqToEntitiesDomainService Dynamic Binding DataBase ,Silverlight RIA 动态绑定数据库

手上有个搜索引擎的项目,每个行业都有一个独立的数据库,这些数据库schemas都是一样。这样RIA就需要根据客户选择的不同行业连接到不同的数据库。解决办法参看 链接

这个方法不是最理想的,大致代码如下

在silverlight端

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

在RIA端override
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);      
}

原文地址:https://www.cnblogs.com/mjgb/p/1956978.html