yo chauhan
yo chauhan

Reputation: 12295

DbContext Object in EntityFramework

In Business Logic Layer i do have a process that has 8 steps . And in DAL am using EntiyFrameWork Code first approach. I have created an object of Container that inherits DBContext like(using(var context=new MyContainer)) in every method where i have to do DBoperaions. For performance part i saw it took time when i create its object. Can i make Object of that container once in Bll (at the beginning of process) and pass it to all the methods as parameter and dispose that object at the end of the process after completing all the 8 steps. Any help will be highly appericiated.

Upvotes: 0

Views: 199

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126547

Yes, you should create exactly one ObjectContext per unit of work.

You can either pass the OC to the steps, or just the parts needed. E.g., if you only need to query MyEntities.Foos for some step, you can do:

public class Step4 
{
    public Step4(IQueryable<Foo> foos)
    {
        // ...

...and then do:

var step4Result = new Step4(context.Foos).Execute();

This will be easier to test than passing the whole context.

Upvotes: 2

Related Questions