开发者

Entity Framework - Scoping context in a web app

I have implemented a simple repository pattern for the Entity Framework in a web app.

I have several repositories which all subclass a base which has some common methods in

The base looked like this

public class BaseRepository<TEntity> : IRepository<TEntity>
{
    protected readonly RedirectsEntities Context;

    public BaseRepository()
    {
        Context = new RedirectsEntities();
    }

(RedirectsEntities is the EF datacontext, or whatever it's called)

And I had a RuleRepository and a SiteRepository which subclassed it

However, this caused me problems when looking up a site and using that value to save to a rule

the error was

"ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"

Presumably because each repository has a different instance of RedirectsEntities ?

So I found this question : ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker

which suggests moving the datacontext to a seperate cl开发者_StackOverflow中文版ass, holding it in a a static variable

e.g.

public class DataContext
{
    private static RedirectsEntities _dbEntities;
    public static RedirectsEntities DbEntities
    {
        get
        {
            if (_dbEntities == null)
            {
                _dbEntities = new RedirectsEntities();
            }
            return _dbEntities;
        }
        set { _dbEntities = value; }
    }
}

and then my base repository constructor would look like this :

public BaseRepository()
{
     Context = DataContext.DbEntities;
}

So this appears to have solved my problem, but I am concerned that the scope of RedirectsEntities is now incorrect.

Can anyone comment on this?


In web applications the most common solution is to scope the context per http request. You initialize the context at the beginning of the request and dispose it at the end. During the request you can save the context in the session state.

If you use a Inversion of Control (IoC) container you can have your container take care of saving the context during the request and supply it to your repositories.

Update

If you are not using a IoC I would create the datacontext at the beginning of the request and put it in the session state. Then I would change the constructor of your base repository to take a instance of your datacontext as a parameter. Then each time you create a repository you get the context from the session and supply it to your repository. And at the end of the request you get the context from the session and dispose it. This way, all your repositories will share the same context over one request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜