Passing Connection string to the JetEntityFramework

267 Views Asked by At

Using the System.Data.SqlClient provider it is possible to pass a connection string to the DbContext constructor as follows;

    public DataContext() //DbContext
    {
        string ConnString = "Connection String Here";
        this.Database.Connection.ConnectionString = ConnString;   

        Database.SetInitializer<DataContext>(null);
    }

Is there a similar way of passing the connection string to the DbContext for the JetEntityFrameworkProvider without using the app.config settings?

1

There are 1 best solutions below

0
On

In your code, the statement Database.Connection.ConnectionString = ConnString; creates a Database.Connection using the App.Config (so probably SQL Server in your case) without open it. Then you set ConnectionString of just created connection that is a SQL Server connection. You can change the App.Config and create always a JetConnection or, if you need to connect to different database types, you can use a different overload of DbContext

public Context(DbConnection connection)
    : base(connection, true)
{ }

To use this overload the connection to Microsoft Access Database must be a JetConnection.