Change the Context according to the application

4

I need the web part to point out which context the system will use.

My project is divided as follows:

AftercompletingitIhadto"replica it", but using another bank. The tables are the same does not change anything, but it is in another bank because it is another system. I just need to change the web.config connection string. I find it "VERY WRONG" to leave two IDENTICAL projects with only the different connection string.

namespace RepositorioEF
{
public class Contexto : DbContext
{
    public DbSet<Cartas> Carta {get;set;}
    public DbSet<Clientes> Clientes { get; set; }
    public DbSet<Loja_Carrossel> Carrossel { get; set; }
    public DbSet<Loja_Menus> Menu { get; set; }
    public DbSet<Loja_Produtos> Produto { get; set; }
    public DbSet<Loja_Submenus> Submenu { get; set; }
    public DbSet<Pedidos> Pedido { get; set; }
    public DbSet<Pedidos_Itens> PedidoItem { get; set; }
    public DbSet<Pedidos_Status> PedidoStatus { get; set; }
    public DbSet<Loja_Codigo> Codigo { get; set; }
    public DbSet<Loja_Usuarios> Usuarios { get; set; }
    public DbSet<Loja_ActionsLog> ActionsLog { get; set; }


    public Contexto()
        : base("BancoDados")
    {
        Database.SetInitializer<Contexto>(null);
    }
}
}

This is the class of my context and I do not know how to make an "IF" for that option. I thought of adding another constructor to use the other connection string, but I do not know if it's the right way.

And I call the context with this class:

namespace RepositorioEF
{
public class ClientesRepositorioEF : IRepositorio<Clientes>
{
    private readonly Contexto contexto;

    public ClientesRepositorioEF()
    {
        contexto = new Contexto();
    }
}
}
    
asked by anonymous 02.07.2014 / 17:22

2 answers

4

Just have more options in the constructor of your context:

public Contexto()
    : base("BancoDados")
{
    Database.SetInitializer<Contexto>(null);
}

public Contexto(String nomeDaConnectionString)
    : base(nomeDaConnectionString)
{

}

Pass the name of ConnectionString at instantiation time:

public class ClientesRepositorioEF : IRepositorio<Clientes>
{
    private readonly Contexto contexto1;
    private readonly Contexto contexto2;

    public ClientesRepositorioEF()
    {
        contexto1 = new Contexto();
        contexto2 = new Contexto("MinhaSegundaConnectionString");
    }
}

To use the default% color, just do not pass anything as an argument.

    
02.07.2014 / 18:45
2

Use Web.config Transformation Syntax to change the connection strings when published:

In the solution explorer, search for: Web.Release.config and change the session you want:

<connectionStrings>
    <add
        xdt:Locator="Match(name)" xdt:Transform="Replace"
        name="{nome-da-connection-string}"
        connectionString="{connection-string-nova}" providerName="System.Data.SqlClient" />
</connectionStrings>

In the publication settings select the desired profile:

Youcancreateasmanyprofilesasyouwant:

And add new transformations to Web.config:

    
02.07.2014 / 19:11