【问题标题】:How to separate between debug and release for connections etc in mvc4如何在 mvc4 中分离调试和释放连接等
【发布时间】:2013-03-20 08:17:21
【问题描述】:

所以我是相当新的 MVC4,许多模式对我来说都是新的。

不过,我很好奇的一件事是关于发布/调试模式的最佳实践。 对我来说,实时模式和调试模式之间有很多不同之处,我希望所有这些都是自动的,因此我不需要更改任何内容即可发布。

例如,我在我的 repo(域项目)中做了这样的事情 公共类 EFAccountRepository : IAccountRepository { 私有 EFDbContext _context;

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

在我的 DI (webui) 中就像这样

#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是什么意思?

热烈欢迎任何其他自动化调试/发布发布的技巧。

【问题讨论】:

    标签: entity-framework deployment release release-management profiles


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      我强烈建议不要将连接字符串硬编码到代码中。请考虑将您的代码指向 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

      【讨论】:

        【解决方案3】:

        如果您有多个连接字符串,则所选答案将不起作用。在发布配置中为所有连接字符串添加以下标签

        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"

        这是我的 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)"/>
        

        【讨论】:

        • 我不确定我是否遵循。如果您在连接字符串 级别有替换转换标记,那么它将完全替换有问题的单个连接字符串。但是,如果您将其移至 级别,它将清除所有 conn 字符串。
        猜你喜欢
        • 2020-10-13
        • 2011-09-02
        • 1970-01-01
        • 2012-09-09
        • 2012-06-03
        • 2012-03-31
        • 2013-03-08
        • 1970-01-01
        • 2014-05-07
        相关资源
        最近更新 更多