none
Use of an entity outside of context RRS feed

  • Question

  • I am learning EF and I don't know what would be the best approach for this:

    I have a Menu entity, which holds the menu of a restaurant in a POS application. In the Order form, I only need to read the menu once, and after that I just need it to display the menu items as buttons for the order taking process.

    Is it OK to use a context to read this menu object on form load for example, and close the context after that? Will that object be available after closing the context?

    Is this a good approach or is a better / recommended way to achieve this? Maybe a wrapper class?

    Thanks!

    Tuesday, February 17, 2015 6:33 AM

Answers

  • Open the connection and close it as soon as you have read the data.
    Tuesday, February 17, 2015 7:31 AM
  • Hello,

    >>Is it OK to use a context to read this menu object on form load for example, and close the context after that?

    I agree with darnold, since you just read data from database one time, it is recommended to close it.

    >>Will that object be available after closing the context?

    It depends on how and where you define this object, if in the context as:

    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        Order order = db.Orders.FirstOrDefault();
    
    }
    

    Assuming the Order is the Menu in your case, after you dispose the context, it would be disposed as well.

    If defining it out of the using syntax, yes, it is still available.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 2:31 AM
    Moderator

All replies

  • Open the connection and close it as soon as you have read the data.
    Tuesday, February 17, 2015 7:31 AM
  • Hello,

    >>Is it OK to use a context to read this menu object on form load for example, and close the context after that?

    I agree with darnold, since you just read data from database one time, it is recommended to close it.

    >>Will that object be available after closing the context?

    It depends on how and where you define this object, if in the context as:

    using (DFDBEntities db = new DFDBEntities())
    
                    {
    
                        Order order = db.Orders.FirstOrDefault();
    
    }
    

    Assuming the Order is the Menu in your case, after you dispose the context, it would be disposed as well.

    If defining it out of the using syntax, yes, it is still available.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 18, 2015 2:31 AM
    Moderator