Is it possible to instantiate a connection to the database manually using the Entity Framework?

1

I would like to manage my connections manually, so that users could enter the server address, but Entity creates the connection string directly in the config file, the only way would be to change that file every time.

But I wanted to do this through code, as if instantiating a connection manually or just informing the connection string via code without modifying the settings file. Is it possible?

    
asked by anonymous 09.08.2017 / 00:21

1 answer

1

Yes, it is possible.

One way to do this is by passing the context constructor parameter. . "> One of the constructor overloads accepts a string .

With this approach it is possible to work in two ways: 1) keeping all connections in the web.config file and building the context by passing the connection name as a parameter; and 2) pass as a string constructor constructor parameter.

public class SeuContexto : DbContext
{
    public SeuContexto(string nameOrConnectionString) : base(nameOrConnectionString)
    {
    }
}

And the use could be something like

var context = new SeuContexto("uma string de conexao gigante aqui");
    
09.08.2017 / 00:36