1

所以我是相當新的MVC4和許多模式對我來說是新的。如何在調試和釋放連接等之間分開mvc4

但是我很好奇的一件事是關於發佈/調試模式的最佳實踐。 對於我來說有很多不同的實況和調試模式,我希望所有這些都是自動的,所以我不需要改變任何要發佈的東西。

因此,例如,我在我的回購協議(領域工程) 公共類EFAccountRepository做過這樣的:IAccountRepository { 私人EFDbContext _context;

public EFAccountRepository() 
    { 
#if DEBUG 
     _context = new EFDbContext("name=Debug"); 
#else 
     _context = new EFDbContext("name=Live"); 
#endif 
    } 

像這樣在我的DI(Web用戶界面)

#if DEBUG 
     EFDbContext efcontext = new EFDbContext("name=Debug"); 
#else 
     EFDbContext efcontext = new EFDbContext("name=Live"); 
#endif 

或者,它會更加聰明,能簡單地擁有

EFDbContext efcontext = new EFDbContext("name=MyApp"); 

然後用web.config中改變變換什麼MyApp的意思嗎?

任何其他自動調試/發佈發佈的提示都受到熱烈歡迎。

回答

5

我會強烈建議不要硬編碼的連接字符串到你的代碼。請考慮將您的代碼指向web.config轉換。您可以在其中添加連接字符串,根據代碼版本,可以應用適當的轉換,以便您只需在應用中使用一次以下代碼即可涵蓋所有環境。

ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString 

裏面的你可以有類似的調試版本

<configuration xmlns:xdt="..."> 
    <connectionStrings> 
     <add name="MyConnectionString" connectionString="debugstring" 
     providerName="debugprovider" /> 
    </connectionStrings> 
</configuration> 

裏面你的發行版,你可以告訴變換,以取代舊的字符串作爲這樣

<configuration xmlns:xdt="..."> 
    <connectionStrings> 
     <add name="MyConnectionString" connectionString="newstring" 
     providerName="newprovider" 
     xdt:Transform="Replace" /> 
    </connectionStrings> 
</configuration> 

更多參考退房 http://msdn.microsoft.com/en-us/library/dd465326.aspx

+1

感謝的人。偉大的作品 –

4

如果您有多個連接字符串,則所選答案將不起作用。在發佈配置下面添加標籤爲所有的ConnectionStrings

XDT:轉換=「SetAttributes」 XDT:定位器=「匹配(名稱)」

這是我在我的Web配置文件

在Web.Debug.Config

<add name="MyConnectionString" 
    connectionString="Data Source=dev;Initial Catalog=DevDB;Integrated Security=True; 
    providerName="System.Data.SqlClient"/> 

和Web.Release.Config

<add name="MyConnectionString" 
    connectionString="Data Source=LIVESERVER;Initial Catalog=DB98;Integrated Security=True; 
    providerName="System.Data.SqlClient" 
    xdt:Transform="SetAttributes" 
    xdt:Locator="Match(name)"/> 
+0

我不知道我關注。如果在連接字符串 level處具有replace變量標記,那麼它將純粹替換正在討論的單個連接字符串。但是,如果您將其移至級別,則會清除所有連接字符串。 –

相關問題