locked
Multiple context and entity framework. RRS feed

  • Question

  • Hi everyone,

    I have a WCF application (sessionMode required, so every client has its own session) that serves multiple clients. Wcf app works against a MsSql database through EF5 (unitOfWork & repository patterns used); I develop an architecture model that for every session I have one EF context. This obviously generate concurrency and refresh problems on entities cached by the context: now if I have an entity modified in db by a user, others can't see that modify because other contexts continue to retrieve old data cached by them. I'm seeing a lot of solutions, very dispersive for me, in Internet.

    So I ask: what is the better solution for these problems? I am relatively new with EF, so I'm spending a lot of time to find THAT solution....

    Thanks in advance,

    Gianni Ghezzi.


    Gianni Ghezzi

    Monday, October 29, 2012 10:49 AM

Answers

  • Dear Alexander,

    thanks for your reply again. I've found a solution, I think it can be a good compromise: I shorten the lifetime of the context, according to some best practices I found in Internet. In my project I use repository and unit of work pattern; the lifetime of the context was equal to that of the session of Wcf; now is strictly sufficient to serves a single method of every session, after that the context is disposed using GC.SuppressFinalyze(context), hoping that this can helps in performance. Sure, I have some overload (every method requests for a new context) but now I haven't no more concurrency or it can be generated sometimes, resolved using the dbconcurrencyexception.

    Thanks a lot for your replies, I hope this can be useful for everyone who looks for similar problems.

    Gianni Ghezzi.


    Gianni Ghezzi

    • Marked as answer by Gianni Ghezzi Saturday, November 3, 2012 5:09 PM
    Saturday, November 3, 2012 5:08 PM

All replies

  • Just for add some knowledge: by further investigatios I'm trying to use Optimistic Concurrency adding a rowversion column to my tables and setting the concurrency mode to Fixed in the related EF entity's column. By this I can reload from db the entity if it was modified by someone between the first read from db (this causes the loading of the entity in EF context), and the successive resend to the context, thanks to EF that generates a concurrency exception (where I force EF to reload that entity).

    Please note that my entity has related entities (like one Order -> many Order details) and for the seconds I use foreign keys in my db (and navigation properties in related EF objects); in this case if someone like above delete or modify an order detail I can see it (by a concurrency exception when deleted or re-update with my data when modified) but nothing happens if someone add another order detail; in db there are for example three order details and I continue to see only two.... how can I intercept this case? I try to add tracking also on Order details table but nothing change. Can someone help me?

    Thanks in advance, Gianni Ghezzi.


    Gianni Ghezzi

    Tuesday, October 30, 2012 6:44 PM
  • Hi Gianni,

    Welcome to the MSDN forum.

    Did you try to reload entities? Please take a look at this page: http://msdn.microsoft.com/en-US/data/jj592904

    Note: Resolving concurrency issues when you are using independent associations (where the foreign key is not mapped to a property in your entity) is much more difficult than when you are using foreign key associations. Therefore if you are going to do concurrency resolution in your application it is advised that you always map foreign keys into your entities. All the examples above assume that you are using foreign key associations.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 31, 2012 3:16 AM
  • Dear Alexander,

    I use foreign keys and navigation properties in my project starting from the database (MsSqlExpress or LocalDB), but reloading of the entity occurs when EF detect a DB concurrency exception; in the case exposed in my second post above, EF doesn't detect anything, so I have more rows (read entities) in database than in the context. Perhaps every time the user accesses to EF I have to refresh all the context or I have to change the lifetime of the context (now it has a lifetime equivalent to the WCF session and this lifetime is from the login to the logout of the user's client to WCF) ?

    Thanks in advance, Gianni Ghezzi.


    Gianni Ghezzi

    Wednesday, October 31, 2012 9:22 AM
  • Hi Gianni,

    OK. I got your point. Since the entity framework does not support Concurrency Mode of navigation properties, you cannot treat navigation properties as normal scalar properties. For your requirement, you can override the SaveChanges method to detect the changes of navigation properties (check if the count of navigation property objects are the same or not, if all the navigation property objects exist in database or not, and if they have the same value in each property). If the navigation properties objects have the navigation properties, the problem became complex I think. Also, you are required to consider the performance problem that whether the detection is better compared with reloading.

    I hope this helps.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 1, 2012 9:31 AM
  • Dear Alexander,

    thanks for your reply again. I've found a solution, I think it can be a good compromise: I shorten the lifetime of the context, according to some best practices I found in Internet. In my project I use repository and unit of work pattern; the lifetime of the context was equal to that of the session of Wcf; now is strictly sufficient to serves a single method of every session, after that the context is disposed using GC.SuppressFinalyze(context), hoping that this can helps in performance. Sure, I have some overload (every method requests for a new context) but now I haven't no more concurrency or it can be generated sometimes, resolved using the dbconcurrencyexception.

    Thanks a lot for your replies, I hope this can be useful for everyone who looks for similar problems.

    Gianni Ghezzi.


    Gianni Ghezzi

    • Marked as answer by Gianni Ghezzi Saturday, November 3, 2012 5:09 PM
    Saturday, November 3, 2012 5:08 PM