Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using different databases for Debug and Release on Entity Framework Code First (DbContext)

I've created the app database using MSDN Tutorial, but now i'm struggling trying to have 2 separate databases, one for when i'm debugging and another for releases, because it doesn't add any app.config line for the connection string...

When i open the properties for the database at server explorer i can see that the actual connection string is Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MediaManager.Model.Context;Integrated Security=True, but i can't differentiate it when in debug or release mode...

How can i do it?


App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MediaManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MediaManager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <userSettings>
    <MediaManager.Properties.Settings>
      ...
    </MediaManager.Properties.Settings>
  </userSettings>
  <system.net>
    <defaultProxy useDefaultCredentials="true" />
  </system.net>
  <applicationSettings>
    <MediaManager.Properties.Settings>
      ...
    </MediaManager.Properties.Settings>
  </applicationSettings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Context.cs

using System.Data.Entity;

namespace MediaManager.Model
{
    public class Context : DbContext, IContext
    {
        public virtual DbSet<Episodio> Episodio { get; set; }

        public virtual DbSet<Serie> Serie { get; set; }

        public virtual DbSet<SerieAlias> SerieAlias { get; set; }

        public virtual DbSet<Feed> Feed { get; set; }
    }
}
like image 819
Gabriel Duarte Avatar asked Jan 29 '26 17:01

Gabriel Duarte


1 Answers

You should be able to simply update your Context to pass in the name of a connection string, and define this connection string in your web.config.

For example:

public class Context : DbContext, IContext
{
    public Context() : base("MyContext") { }

    public virtual DbSet<Episodio> Episodio { get; set; }

    public virtual DbSet<Serie> Serie { get; set; }

    public virtual DbSet<SerieAlias> SerieAlias { get; set; }

    public virtual DbSet<Feed> Feed { get; set; }
}

Then in the web.config

<configuration>
    <connectionStrings>
        <add name="MyContext" connectionString="..your connection string.." providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Once this is in place, you can then use Web Configuration Transforms to automatically update the connection string for a Release build vs. a Debug build.

like image 76
Brendan Green Avatar answered Jan 31 '26 05:01

Brendan Green