简体   繁体   中英

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.

I want to create Update-Database commands for dev and production that use a specified connection provided in the command.

I ran PM> get-help Update-Database -detailed but did not see any relevant examples for what I am trying to do.

My question: What params need to be in the 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?

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

In your Web.Release.config file you would have an entry like:

<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

1) Bare minimum is:

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

2) Default values will be used for other args not specified:

https://msdn.microsoft.com/en-us/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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