简体   繁体   English

带有-ConnectionString的实体框架更新数据库

[英]Entity Framework Update-Database with -ConnectionString

Our web,config uses the same connection name for dev and production, with the only difference being the connection string itself. 我们的web,config使用相同的连接名称进行开发和生产,唯一的区别是连接字符串本身。

I want to create Update-Database commands for dev and production that use a specified connection provided in the command. 我想为开发和生产使用命令中提供的指定连接创建Update-Database命令。

I ran PM> get-help Update-Database -detailed but did not see any relevant examples for what I am trying to do. 我运行了PM> get-help Update-Database -detailed但是没有看到我尝试做的任何相关示例。

My question: What params need to be in the ConnectionString? 我的问题:ConnectionString中需要包含哪些参数? Obviously I need initial catalog, data source, user id and password. 显然,我需要初始目录,数据源,用户名和密码。 Not sure about the others, though. 不过,不确定其他。 Do I even need the connection name? 我甚至需要连接名称吗?

        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 

Have you thought about using web.config transformations instead? 您是否考虑过改用web.config转换?

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

In your Web.Release.config file you would have an entry like: 在您的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>

Using xdt:Transform="SetAttributes" means that you don't need to specify the provider again, only the connection string will change in your transformed web.config 使用xdt:Transform="SetAttributes"意味着您无需再次指定提供程序,只需在转换后的web.config中更改连接字符串即可

1) Bare minimum is: 1)最低裸机为:

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

2) Default values will be used for other args not specified: 2)默认值将用于其他未指定的参数:

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

3) ConnectionStringName is required only if you want connection string be taken from connection string defined in your web.config or app.config 3)仅当您希望从web.config或app.config中定义的连接字符串中获取连接字符串时,才需要ConnectionStringName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM