Entity Framework with Owin DbContext for query in MVC

I noticed that the project template for MVC when using separate user accounts puts several objects in the current Owin context (in App_Start/Startup.Auth.cs):

// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

This seems to be database access for Identity features. I understand that one instance ApplicationDbContextis created for each request and reused across the pipeline. Would it be helpful to do the same with my own entity DbContexts infrastructure ?

For example, I created a new file in App_Start/Startup.Data.cs:

public partial class Startup
{

    public void ConfigureData(IAppBuilder app)
    {

        app.CreatePerOwinContext(CreateParkingEntities);

    }

    protected ParkingEntities CreateParkingEntities()
    {
        return new ParkingEntities();
    }

}

Then in Startup.cs:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        ConfigureData(app);
    }
}

Then I can use the context in my controllers:

private ParkingEntities _db;

public ParkingEntities DbContext
{
    get
    {
        return _db ?? HttpContext.GetOwinContext().Get<ParkingEntities>();
    }
    private set
    {
        _db = value;
    }
}

, , , . , DbContext , Owin, ?

, - DbContext, .

+4
2

DbContext , . , .

, Find() DbSets, , , , . , , , .

DbContext , , . , - , , .

0

EF db-context : db-, db-context. EF IDisposable ( () {}). , EF , db-. db-context , db. . , db, , db-context owin- .

0

All Articles