2014-09-10 46 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

所以,当有人忘了(有人会)要设置“释放”或“调试”模式,您的系统将错误地触发开发数据或生产数据,这似乎是一种灾难性的处方......这对我来说似乎并不算失败。必须有一种方法将其编码到应用程序中。例如,您可以编写代码,以便应用程序确定它正在运行的服务器,如果它是生产服务器,则使用生产连接字符串....如果它是测试服务器,则使用测试连接字符串,否则使用开发连接字符串。