Change Context according to application

Asked

Viewed 731 times

4

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

My project is divided as follows:

Projeto

After its completion I had to "replicate" it, but using another bank. The tables are equal does not change anything, but it is in another database because it is another system. I just need to change the connection string of the web.config. I find "VERY WRONG" to leave two projects IDENTICAL only with 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 don’t know how to make an "IF" for a certain option. I thought about adding another constructor to use the other string Connection, but I don’t know if it is the most appropriate way.

And I call context with this class:

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

    public ClientesRepositorioEF()
    {
        contexto = new Contexto();
    }
}
}
  • see if I got it right: you created a project and want to use the same project structure for another project. That’s it?

  • That.. Just change the database.

2 answers

4


Just have more options in your context constructor:

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

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

}

Pass the name of ConnectionString at the time of instantiation:

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 ConnectionString standard, just pass nothing as argument.

  • Could you keep only the second constructor of Context? " public Context(String nameConnectionString) : base(nameConnectionString)" I think it’s kind of redundant, and I can pass the database by parameter.

  • @Diegozanardo Can. Just make sure to specify default values.

  • What do you mean? Default values?

  • If you pass one String empty, the code has to work. Just make sure that.

2

Utilize Web.config Transformation Syntax to change the Connection strings when published:

In Solution explorer, search for: Web.Release.config and change the desired session:

<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 post settings select the desired profile:

Selecionando profile na publicação de uma app web

You can create as many profiles as you want:

Novas configurações

And add new transformations to Web.config:

Adicionar novas transformações para o Web.config

  • But then I would still have two projects. It would be more interesting to configure this via code, for the system to identify and change.

  • @Diegozanardo, you would have just one project with N Web transformations.config.

  • @Ridermansb I think what he wants is to have two contexts working at the same time on different bases. The transformation is another aspect, referring more to the publication of the project in production.

  • I understood his argument, I would have to make several publishs for each client (which is not ideal). In this case the ideal would be via same code.

  • @Gypsy omorrisonmendez Exactly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.