1

in c# entityframework i do creating new instance of context like this:

using(var context=new dataaccess.MyObjectContext()){
  ....
}

where MyObjectContext is like this:

public partial class GlobalContext : ObjectContext
{
 public GlobalContext()
        : base("name=MyDbContext")
    //: base(ConnectionString)
    {
        CommandTimeout = 2000;

        _AssetCategory = CreateObjectSet<AssetCategory>();
        _AssetItem = CreateObjectSet<AssetItem>();
        _AssetItemType = CreateObjectSet<AssetItemType>();
        _AssetItemMUnit = CreateObjectSet<AssetItemMUnit>();
        _FileDB_Asset = CreateObjectSet<FileDB_Asset>();
}

    private ObjectSet<AssetCategory> _AssetCategory;
    public ObjectSet<AssetCategory> AssetCategory
    {
        get { return _AssetCategory; }
    }

    private ObjectSet<AssetItem> _AssetItem;
    public ObjectSet<AssetItem> AssetItem
    {
        get { return _AssetItem; }
    }

}

} Is it true that I create new instance anytime that I want call query?

3
  • 2
    The answer to your question is "Yes the code you show does do that" however I doubt that is what you actually wanted to know. Is your real question "Do I need to create a new instance from EntityFramework ObjectContext anytime that I want call query?" Apr 14, 2015 at 19:09
  • yes , my question is . Apr 14, 2015 at 19:27
  • If that is your real question then edit your question to say so. Apr 14, 2015 at 19:28

3 Answers 3

1

The Context object is IDisposable so you have to dispose of it as soon as you finish working with it usually by surrounding it in a using construct. Or make sure that the class you are using the Context in is also properly implementing IDisposable and it disposes the Context too.

2
  • my context involves of many OjbectSet , in this situation creating new instance anytime get memory , in fact if my users to be too many , the memory that iis get go up, is this true? Apr 15, 2015 at 3:36
  • You have to test that to be sure if the current infrastructure can handle the load. You may even got to the point where you abandon EF and start using something else like dapper (stackoverflow uses dapper for accessing the db). I've too abandoned EF a long time ago and created my own custom tool to handle db stuff a lot more faster. Apr 15, 2015 at 10:06
0

No. You should create a new object context each time you use it. It can indeed contain multiple queries.

Mind you that you shouldn't leave an object context opened for a long time, for instance in a desktop application. This is because changes that are made by others are not visible to your current object context.

In a website, you can define it as a global object, because a website usually is done rendering in no time. Define it in a way that it gets created each time you have a GET or POST request and dispose of it when the website is done rendering.

5
  • In fact you say that I should create new instance any time ? Apr 14, 2015 at 19:16
  • "In a website, you can define it as a global object" - what do you mean by 'global object'? If you mean a singleton or static, then you are incorrect. If fact, a singleton or static context doesn't make sense anywhere.
    – Maarten
    Apr 14, 2015 at 19:40
  • Any time you use it. You can use it for as many queries as you want, but when you're done, dispose of it. Or simpler: In a method where you need it, define it once and use it.
    – bytecode77
    Apr 14, 2015 at 19:42
  • 1
    "a website usually is done rendering in no time" - that is not true.
    – Maarten
    Apr 14, 2015 at 19:43
  • Yes. It means that a postback takes less than a second and it's done. So you can define your object context in a way that it gets created each time you have a POST or GET request.
    – bytecode77
    Apr 14, 2015 at 19:44
0

You should be disposing the Context at the end of it's intended life cycle. The life cycle might be based on per request (web) or span the entire lifespan of the desktop application (single user desktop and no other db users, for example). You should also build your service/logic layer so that it is not defining the life cycle of the Context, instead it should be based on the application itself on when to dispose the Context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.