[英]Silverlight WCF RIA Services Dynamically Change Connection String
我們有一個Silverlight應用程序,它使用WCF RIA服務和Entity Framework 4.1連接到數據庫。
目前,已提供連接字符串,這是web.config中的標准配置,並且都可以成功運行。
但是,我現在希望能夠在運行時基於用戶登錄等動態地更改連接字符串。
我發現了其他一些暗示可以做到這一點的帖子 ,但是它們使用的是ObjectContext,而我們在System.Data.Entity命名空間以及DbDomainService類中使用的是DbContext。
為了彌補這一點,我在DbDomainService實現中重寫了CreateDbContext()方法,如下所示:
protected override CoreContext CreateDbContext()
{
dbServer = null, dbName = null;
httpCookie = System.Web.HttpContext.Current.Request.Cookies["DBServer"];
if (httpCookie != null)
{
dbServer = httpCookie.Value;
}
var cookie = System.Web.HttpContext.Current.Request.Cookies["DBName"];
if (cookie != null)
{
dbName = cookie.Value;
}
if (dbServer == null && dbName == null)
{
return new CoreContext();
}
string connStr = "Data Source=" + dbServer + ";Initial Catalog=" + dbName + ";Integrated Security=true";
return new CoreContext(connStr);
}
首次加載Silverlight應用程序時,此操作成功完成,但是,在所有后續加載中,盡管更改了替換為連接字符串的值,但仍使用與最初建立的連接相同的連接。
更改連接的唯一方法似乎是回收IIS中的應用程序池並再次加載該應用程序。
難道我做錯了什么? 還是無法讓DbDomainService動態更改其連接?
我在考慮您的domainservice類的實例化模型。 您是否嘗試過自定義IDomainServiceFctory ? 它使您可以決定何時創建它們的新實例,並且實現起來非常簡單。
也看看這個帖子由Fredrik諾門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.