Assuming there is an ASP.NET MVC application that uses Entity Framework 6 with code-first approach and StructureMap as IoC.
Also It uses Unit Of Work pattern.
Here are the c
In my experience, I used the Database First
mode in EF 6. The DbContext
would be generated like below when I add Entity Data Model
.
public TestEntities()
: base("name=TestEntities")
{
}
The TestEntities
represent the ConnectionString
element in the App.Config
But you can change the default code to below.
public partial class TestEntities : DbContext
{
public TestEntities()
: base("name=TestEntities")
{
}
public TestEntities(string sConnectionString)
: base(sConnectionString)
{
}
...}
So you got two options to getting DB connection.
using the default. The EF will find the connection string in the config file.
passing the connection string to DbContext.
The code look like below.
EntityConnection entityConn =DBConnectionHelper.BuildConnection();
using (var db = new TestEntities(entityConn.ConnectionString))
{
....
}
As to the question How to build a EntityConnection?
. Please see MSDN EntityConnection.
Hope it is helpful.
Thanks.