如何在mvc4中分离调试和释放连接等 [英] How to separate between debug and release for connections etc in mvc4

查看:149
本文介绍了如何在mvc4中分离调试和释放连接等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am fairly new MVC4 and many patterns are new to me.

然而,我很好奇的一件事是关于发布/调试模式的最佳做法。
对于我来说,有一些事情在实时和调试模式之间有所不同,我希望所有这些都是自动的,所以我不需要更改任何内容来发布。

However the one thing I am curious about is best practice about release/debug modes. There are a bunch of things for me that differ between live and debug mode and I would like for all of them to be automatic so I don't need to change anything to publish.

所以例如我在我的repo(域项目)中已经这样做了
public class EFAccountRepository:IAccountRepository
{
private EFDbContext _context;

So for instance I have done like this in my repo (domain project) public class EFAccountRepository : IAccountRepository { private EFDbContext _context;

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

并在我的DI(webui) / p>

and like this in my 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的意思吗?

And then change with web.config transform what MyApp means?

热烈欢迎任何其他自动调试/发布 - 发布的提示。

Any other tips of automating debug/release-publish is warmly welcomed.

推荐答案

我强烈建议不要将您的连接字符串硬编码到代码中。请考虑将您的代码指向一个web.config转换。您可以在其中添加连接字符串,并根据代码版本可以应用适当的转换,以便您只需在应用程序中使用以下代码来覆盖所有环境。

I would strongly recommend not hardcoding your connection strings into your code. Please consider pointing your code to a web.config transform. You can add the connection string there and depending on the version of code the proper transform can be applied so that you simply need to use the following code once in your app to cover all environments.

ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString

在调试版本的内部,您可能会有类似于

Inside of the debug version you could have something similar to

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

在您的发布版本中,您可以告诉转换以替换旧的字符串,因此

Inside your release version you can tell the transform to replace your old string as so

<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

这篇关于如何在mvc4中分离调试和释放连接等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆