none
Entity Framework - one context or multiple RRS feed

  • Question

  • I'm trying to use Code First for my project, which contains waregroups and articles (EF, MVVM, Windows Azure)
    Unfortunately I'm faced with different approaches on my searches and am a bit puzzled on which technique to use accordingly:

    An article always has a waregroup, declared as "public virtual waregroup" in the class.

    Click on "Articles" opens a view with all articles, double clicking an article entry opens the "single view".

    I tried 2 approaches:

    1.) I create an instance of my service class in the "all articles" viewmodel and pass the reference according with the context to the "single viewmodel" - works - but when checking for a "DbUpdateConcurrencyException" later it does not get recognized. (f.e: saving an article where the waregroup has been deleted by another user in the meantime)

    2.) In my service class I work with the "using..." statement to create a context that gets released immedately (I read that "sticking to one context" is bad practice) and the DbUpdateConcurrencyException gets cought and handled. However Lazy Loading now refuses to work of course - because the context doesn't exist any more.
    If I disable lazy loading however - no waregroup gets loaded for the article.

    I found an article about the unit of work pattern
    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
    this doesn't solve my problem however (by the way it seems that the context gets "kept alive" there for a long time when using it for multiple models) because the ConcurrencyExceptions don't get handled in the example.

    I'd be grateful for input, thank you




    • Edited by FloLeBlanc Thursday, April 11, 2013 9:13 AM
    Thursday, April 11, 2013 9:04 AM

Answers

  • Hi FloLeBlanc,

    Here are some guidelines when deciding the lifetime of the context:

    When working with Web applications, use a context instance per request.
    When working with Windows Presentation Foundation (WPF) or Windows Forms, use a context instance per form. This lets you use change-tracking functionality that context provides. 

    However Lazy Loading now refuses to work of course - because the context doesn't exist any more.
    If I disable lazy loading however - no waregroup gets loaded for the article.

    I think you may try explicitly loading in this scenario. If you have got an instance of Article, you can use Load method on the related entitiy's entry.

    context.Entry(article).Reference(a => a.waregroup).Load();

    http://msdn.microsoft.com/en-us/data/jj574232#c_e2bb0d278c9c4f69abca810102599556

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by FloLeBlanc Monday, April 22, 2013 2:41 PM
    Monday, April 15, 2013 2:46 AM
    Moderator

All replies

  • Hi FloLeBlanc,

    Here are some guidelines when deciding the lifetime of the context:

    When working with Web applications, use a context instance per request.
    When working with Windows Presentation Foundation (WPF) or Windows Forms, use a context instance per form. This lets you use change-tracking functionality that context provides. 

    However Lazy Loading now refuses to work of course - because the context doesn't exist any more.
    If I disable lazy loading however - no waregroup gets loaded for the article.

    I think you may try explicitly loading in this scenario. If you have got an instance of Article, you can use Load method on the related entitiy's entry.

    context.Entry(article).Reference(a => a.waregroup).Load();

    http://msdn.microsoft.com/en-us/data/jj574232#c_e2bb0d278c9c4f69abca810102599556

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by FloLeBlanc Monday, April 22, 2013 2:41 PM
    Monday, April 15, 2013 2:46 AM
    Moderator
  • thank you for the input
    Monday, April 22, 2013 2:42 PM