0

Transform web.config won't work well for Entity Framework CodeFirst because the connection string is added dynamically after transform when publishing. We have a dynamic connection settings being set in code for CodeFirst when releasing to different environments.

I have tried the correct removal method but the published results ends up with the connection string in the web.config.

Are there any work arounds for this? Putting connection info in our web.configs isn't an option. Adding the connection info dynamically at runtime works well.

The problem is when publish adds the connection info in the web.config the app gets a 500 error because codefirst is attempting to use an invalid connection string for the environment that is not sandbox where it was created.

We are changing that at runtime here and that is working.

public MyAppDataContext()
{
   this.Database.Connection.ConnectionString = OurDynamicConfigSettings.GetSetting("MyAppConnectionString");
}

The codefirst though is attempting to use what is in web.config before we set it dynamically. The fix is to remove the connection info from the web.config.

It all works except when building on build server or publishing, codefirst inserts the connection info back into the web.config every time we publish. Having to remember to remove this each time is not good practice and prone to errors if you forget.

Code for removing connection string in our transform file should work but doesn’t.

<connectionStrings>
    <add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[@name='MyAppConnectionString'])" />
</connectionStrings>
1
  • You can add a action to the build to remove the line from web.config. You will have to find your build events tab on the project properties. Add an action to the Post-build event command line. Commented Mar 6, 2014 at 21:37

2 Answers 2

2

Your transform should be:

<connectionStrings>
    <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="MyAppConnectionString" />
</connectionStrings>

But, I have to ask.. are connection strings stored in your database or something? Why do you need to do them in code?

You could just do this:

public class MyDataContext : DbContext {
     public MyDataContext(string connectionString) : base(connectionString)

   ...
}

Then when you create you contexts, you do this:

using(var context = 
   new MyDataContext(OurDynamicConfigSettings.GetSetting("MyAppConnectionString"))) {
    ...
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Both transform scripts will work for normal circumstances. The problem isn't the transform script but the fact the connection string is being added by EF Codefirst after transforms are run when you publish. Yes, connection info comes from a secured service so using web.config here is not an option.
@RandallTo - That's simply not true. EF does not add anything to your web.config after publishing. It can't. It's just not possible.
I think it is because of the fact whenever you are using EF Codefirst then the publish or build server adds this. I agree, I don't believe there is actual code running on the build server from EF that does this. The end result is it is being added because of using EF Codefirst.
@RandallTo - No, it's not anything to do with EF. I promise you, it's patently impossible for EF to be adding a connection string after the publish. If it's happening, something else is doing it. Regardless, using the constructor parameter will make EF use the connection string you specify (note this is not the same thing as where you set the connection string in the constructor)
Thanks. That constructor trick works. Sorry, my eyes didn't see that the first time you posted. The transform script you provided also because I can leave the connection string info in the web.config for the project. It was the manual removing it from the project is the reason it was not being removed with transform before. Something is adding it back in though upon publish and build for sure when you remove it from your project, but with these two work arounds it not a problem now.
|
0
 public partial class MyContextEntities : DbContext
    {
        public MyContextEntities(string ConnectionString="[Your Entity Connection String Here]")
            : base(ConnectionString)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
}

Replace " with ' (single quote) in connection string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.