2014-09-10 33 views
0

我一直在研究一個項目,現在有一個生產版本運行在共享主機環境和一個在我的開發機器上。 這裏的問題是,每次我想在我的開發機器上運行應用程序時,我將不得不修改連接字符串設置以指向開發機器上的localDb,反之亦然,當我想要發佈時。 對我來說,這變得非常不方便,直到我想出至少能夠完美工作的解決方案。 我的問題是,這是我的解決方案專業和可靠的Aspnet環境?或者有沒有一種更好的方法可以更好地實現這個解決方案?謝謝。詳情請參閱代碼。AspNet Mvc自動選擇ConnectionString

//Created a static method in Global.asax.cs  

/// <summary> 
    /// Returns Connection settings based on the machine. i.e, automatically select connection strings if it's development or live server 
    /// </summary> 
    /// <param name="ArrayIndex"></param> 
    /// <returns> string BaseConn = (connectionstring name) </returns> 

public static string 

    Settings(int ArrayIndex) 
    { 


     string[] BaseConn ={ HttpContext.Current.Request.IsLocal ? System.Configuration.ConfigurationManager.ConnectionStrings[4].Name : 
     System.Configuration.ConfigurationManager.ConnectionStrings[1].Name, 

//Identity database connections 

     HttpContext.Current.Request.IsLocal ? System.Configuration.ConfigurationManager.ConnectionStrings[5].Name : 
     System.Configuration.ConfigurationManager.ConnectionStrings[2].Name, 

//store database connection 

     HttpContext.Current.Request.IsLocal ? System.Configuration.ConfigurationManager.ConnectionStrings[6].Name : 
     System.Configuration.ConfigurationManager.ConnectionStrings[3].Name, 
           }; 
     return BaseConn[ArrayIndex]; 
    } 

然後我打電話的DbContext類中的方法遵循

public class MyAppDb :DbContext 

{ 


    public MyAppDb() 
     : base(MvcApplication.Settings(0)) 
    { } 

// Some entity tables here 
} 

我做了商店的DbContext和身份的DbContext相同。 在本地和現場機器上,一切看起來都很棒。儘管我無法使用codefirst遷移使用此解決方案更新數據庫。至少現在不是問題。

回答

1

您需要使用Web.Release.Config。

在Web.config中,您將指定自己的(本地)連接字符串,並在發佈時使用類似這樣的內容(根據您自己的sql連接更改配置)。

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <connectionStrings> 
    <add name="MyContext" 
     connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SalvaVidas;Server=ServerName;Database=MyDb;Trusted_Connection=True;Integrated Security=SSPI;" 
     xdt:Transform="SetAttributes" 
     xdt:Locator="Match(name)"/> 
</connectionStrings> 

確保name屬性在兩者中都匹配。在這個例子中,它是:name =「MyContext」

注意:請確保當您發佈時,它將以RELEASE模式進行。

+1

非常感謝您使用基於Codebased的時間。這似乎是標準的做法。我應該使用這個而不是我自己的解決方案,儘管工作沒有任何錯誤。 – codein 2014-09-10 19:51:04

0

您最好使用而不是Web.Config轉換

這個想法是,部署後,您選擇目標環境,配置文件將根據Web.{CONFIGURATION_NAME}.config文件中描述的更改自動進行轉換。

例如,更改連接字符串爲Release配置你會需要這個在Web.Release.config文件:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <connectionStrings> 
    <add name="MyDB" 
     connectionString="{SQL_SERVER_CONNECTION_STRING_FOR_RELEASE}" 
     xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/> 
    </connectionStrings> 
</configuration> 

Tutorial

+0

謝謝你的時間。感謝您的鏈接了。它給了我更多關於這個問題的見解。 – codein 2014-09-10 19:52:28

0

所以,當有人忘了(有人會)要設置「釋放」或「調試」模式,您的系統將錯誤地觸發開發數據或生產數據,這似乎是一種災難性的處方......這對我來說似乎並不算失敗。必須有一種方法將其編碼到應用程序中。例如,您可以編寫代碼,以便應用程序確定它正在運行的服務器,如果它是生產服務器,則使用生產連接字符串....如果它是測試服務器,則使用測試連接字符串,否則使用開發連接字符串。