【问题标题】:Entity Framework Update-Database with -ConnectionString带有 -ConnectionString 的实体框架更新数据库
【发布时间】:2015-03-23 15:20:17
【问题描述】:

我们的 web,config 对 dev 和 production 使用相同的连接名称,唯一的区别是连接字符串本身。

我想为使用命令中提供的指定连接的开发和生产创建更新数据库命令。

我运行了PM> get-help Update-Database -detailed,但没有看到任何与我正在尝试做的事情相关的示例。

我的问题:ConnectionString 中需要包含哪些参数?显然我需要初始目录、数据源、用户 ID 和密码。不过,不确定其他人。我什至需要连接名称吗?

        update-database 
    -ConnectionStringName MyConn 
    -ConnectionString data source=10.10.10.20;
                      initial catalog=MyDatabase; 
                      persist security info=False;
                      user id=my_db_user;
                      password=1234;
                      max pool size=200;
                      MultipleActiveResultSets=True" 
                      providerName="System.Data.SqlClient 

【问题讨论】:

  • 不确定你的意思。两者的连接名称相同。有没有办法通过环境变量来做到这一点?

标签: c# entity-framework entity-framework-migrations


【解决方案1】:

1) 最低限度是:

  • DataSource / Server / Address / Addr / Network Address
  • Initial Catalog / Database
  • User Id / UID + Password / PWDIntegrated Security = true

2) 默认值将用于其他未指定的参数:

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring%28v=vs.110%29.aspx

3) 仅当您希望从 web.config 或 app.config 中定义的连接字符串中获取连接字符串时,才需要 ConnectionStringName

【讨论】:

    【解决方案2】:

    您是否考虑过改用 web.config 转换?

    https://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

    在您的 Web.Release.config 文件中,您将有如下条目:

    <connectionStrings>
        <add name="MyDbName" connectionString="Data Source=10.10.10.20; Initial Catalog=MyDatabase; User Id=my_db_user; Password=1234;"
             xdt:Locator="Match(name)" xdt:Transform="SetAttributes" />
    </connectionStrings>
    

    使用xdt:Transform="SetAttributes"意味着你不需要再次指定提供者,只有连接字符串会在你转换的web.config中改变

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多