WebPublish Code-First Migrations with external connectionStrings.config

I currently have a project that uses Entity Framework Code-First and Web Publish migrations where connectionStrings are stored in web.config file.

It's time to move the connectionStrings outside of web.config, and so we put them in the connectionString.config file, and slowcheetah convert them to webpublish.

connectionStrings.config

<connectionStrings> <!-- Testing Databases --> <add connectionString="server=testserver;database=testdatabasename;user id=someid;password=*******" name="dbname" providerName="System.Data.SqlClient" /> </connectionStrings> 

web.config corresponding section

  <connectionStrings configSource="config\connectionStrings.config"> </connectionStrings> 

Now, when I load a dialog box for Web Publish (Build β†’ Publish Project), on the settings tab I get an error

 No Databases found in the project 

This indicates that the dialog is not smart enough to see configSource and load data from there. I can confirm that connectionStrings are loaded correctly in my developer environment, and I can also confirm that slowcheetah will correctly convert the configuration to a production environment.

Is there a way for Visual Studio Publish to consider configuration configuration and allow code migration?

+5
source share
1 answer

I had the same problem as using Entity Framework 6 code initially in a separate project from a published project under .net 4.5 using Visual Studio 2013 Update 4 and using a similar share method via the configSource connectionStrings attribute.

I list these details because it causes a bit of a perfect storm. Trying to get out of it, I came across several different errors and had to crack it in several ways, for the EF blog they should know that this is a mess and redistribute their approach . This is the best I could come up with (here are the dragons):

The publishing wizard does not understand configSource, so I removed this from conectionStrings in web.config, leaving an empty element (you can also delete it completely, but I felt that the existing but empty element with comment was preferable more). To make it work locally, I added a factory connection so that everything works in debug mode (locally) and makes the publishing wizard find the db migration. I used factory because connectionString always replaces factory, and I need to abuse it so that the project works before any conversions. Also make sure that the string you pass to the dbcontext aka constructor "connectionStringOrDatabaseName" is both the connection string AND the database name (makes the factory generated db match the db connection string).

Now it finds the database and it works locally, but in fact it does not use your sharedConfig (therefore, it will not always work when publishing). To address this, I then used the conversion of web.config to xsd: Replace / Insert a blank / missing connectionStrings element with one using configSource. If you try to publish now, you will encounter a problem when an invalid connectionStrings element is created.

Despite the fact that I was so close, I could not find / figure out a solution to this problem directly, so I had to use one more work: I created a custom transformation aka xsd: Import, which, if this configSource attribute replaces the parent element with the one what is in another file, I will leave the implementation as an exercise for the reader.

+3
source

Source: https://habr.com/ru/post/1210570/


All Articles