【问题标题】:How do I set a default connection string in a console application?如何在控制台应用程序中设置默认连接字符串?
【发布时间】:2014-09-09 18:37:00
【问题描述】:

在我的带有 Web 界面的 MVC 项目中,我习惯于在 Web.Config 文件中设置连接字符串。

但是,现在我正在制作一个沼泽标准控制台应用程序 - 也带有一个数据库挂钩,但是如何为应用程序全局设置连接字符串?

目前,我正在设置

var dbIndex = new DBContext();
dbIndex.Database.Connection.ConnectionString = 
    "Data Source=USER-PC;Initial Catalog=TextProject.DBContext;" + 
    "Integrated Security=True;MultipleActiveResultSets=True";

但我每次都必须在所有函数调用中设置此连接字符串属性。当我没有 web.config 时,有没有办法设置全局连接字符串?

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    正如其他人所说,将您的连接字符串放在App.config 文件中。

    关于实体框架。如果您使用的是实体框架 (EF) 版本 6.2.0,那么早期的 EF 可能会自动查找与继承自 DbContext 的类同名的连接。

    如果你的班级是

    public class MyDatabaseContext : DbContext 
    {
        // other code
    }
    

    EF 将查找名称如下的连接字符串 <add name="MyDatabaseContext" ...

    你也可以像这样在构造函数中设置名字

    public class MyDatabaseContext : DbContext 
    {
      public MyDatabaseContext() : base ("name=defaultConnection")
      {
        // other code
      }
    
      // other code
    }
    

    在这种情况下,您的连接字符串名称可以是<add name="defaultConnection" ...

    【讨论】:

      【解决方案2】:

      解决方案非常简单易行。只需右键单击并将项目设置为启动项目。实体框架默认选择启动项目并使用 App.config 或 web.config 中的连接字符串

      请参考:

      【讨论】:

        【解决方案3】:

        App.config 相当于控制台或 .exe 程序的 Web.config。

        • 在要使用 app.config 的 ConnectionStrings 的任何项目上添加对 System.Configuration 的引用
        • 将 AppConfig 添加到您的入口点项目。这是执行的项目。 (例如控制台 .exe)
        • 在 app.config 的 <connectionStrings></connectionStrings> 部分添加连接字符串

        现在将连接字符串放在你的 app.config 中

        string connStr =ConfigurationManager.ConnectionStrings["ConnName"]
          .ConnectionString;
        

        App.config:

        <?xml version="1.0" encoding="utf-8" ?>
        <configuration>
          <connectionStrings>
            <add name="ConnName" connectionString="Data Source=USER-PC;Initial Catalog=TextProject.DBContext;Integrated Security=True;MultipleActiveResultSets=True" />
         </connectionStrings>
        </configuration> 
        

        【讨论】:

          【解决方案4】:

          所以我认为你的意思是实体框架(我假设你正在使用)寻找defaultConnection 连接字符串。

          您可以尝试按照其他人的建议将其放入 app.config 文件中,但我不确定这是否会被 EF 自动拾取。

          如果它不起作用,你可以做的是创建继承 DbContext 的新类 -

          public class MyDbContext : DbContext
          {
              public MyDbContext() : base()
              {
                  var cs = ConfigurationManager.ConnectionStrings["defaultConnection"]
                                               .ConnectionString;
          
                  this.Database.Connection.ConnectionString = cs;                
              }
          }
          

          【讨论】:

            【解决方案5】:

            您可以将相同的元素放在控制台应用程序的 App.config 文件中,并以与 Web.config 相同的方式访问它。 相关:What is App.config in C#.NET? How to use it?

            【讨论】:

              猜你喜欢
              • 2015-03-02
              • 1970-01-01
              • 1970-01-01
              • 2018-03-13
              • 2010-09-11
              • 1970-01-01
              • 2013-06-25
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多