Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:29:48+00:00 2026-06-01T22:29:48+00:00

According to this post , I am using a data context per call, so

  • 0

According to this post, I am using a data context per call, so in each method of my WCF service, I use a using block to create a new data context.

But I have some doubts in the form to work in this way.

For example, I use a method getAllCLients() from my repository to get all the clients of the data base, then the service send to the client that call the method a list with all the clients. Then the user modify the information of some of them, three for example. The modify client perhaps I can add to a list that have the modified clients.

When I want to update this three clients, I can call a method updateClients() which receive a list of modified clients. How I am use a new data context per each method, in updateCients() get a new dataContext, without entities, so I think that I have to follow this steps:

1.- create a new data context which has the clients that I want to update. SO I need to specified the conditions for that. This is an extra operation (I get the clients before with the getAllClients() method), so I need to get again the clients.

2.- go throw the clients collection of the DBSet (I use EF 4.1) and change the information. This makes me to go throw the list that I receive from the client application too. So I must to go throw two lists. This needs resources.

3.- save the changes. This is needed anyway, so it has no required more work.

There is any way to make the step 2 easily? exist some method in dataContext to pass the values from my modified client to the client in the data context? I use POCO entities, perhaps it exists an easy way to do that.

Other question is about concurrency. If I control the concurrency with pesimistic concurrency that allow EF (with a timestamp field for example), is it better to call the updateClient() one for each client or better to pass a list with all the clients? I mean that if I use a list as parameter, if there is a concurrency issue with one client,the second for example, the first client will be update correctly, but the second not and the third neither. How can I notify to the user that there is problems with some clients?

To resume, I would like to know the best way to make updates when I have a short life datacontext.

Thanks.
Daimroc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:29:50+00:00Added an answer on June 1, 2026 at 10:29 pm

    The service is disconnected scenario so when your client passes backs modified records you just need to process them as modified. You don’t need to load all records from database for that.

    public void SaveClients(List<Client> modifiedClients)
    {
        using (var context = new Context())
        {
            modifiedClients.ForEach(c => 
               {
                   context.Entry(c).State = EntityState.Modified;
               });
            context.SaveChanges(); 
        }
    }
    

    If you are using per call service and every service operation needs context you can move your context instancing to service constructor because service instance will live only to server single service call = you don’t need using for every call. If you do that don’t forget to implement IDisposable on your service to dispose context.

    Other question is about concurrency. If I control the concurrency with
    pesimistic concurrency that allow EF (with a timestamp field for
    example), is it better to call the updateClient() one for each client
    or better to pass a list with all the clients?

    EF doesn’t support pesimistic concurrency out of the box. Using timestamp is optimistic concurrency because it allows others to use the record. Pesimistic concurrency is application logic where other client is not able to select locked record for update.

    The concurrency is resolved per record but the problem in this case is transaction. Each call to SaveChanges results in transaction used to process all changes in the database. So if any of your modified records is not up to date you will get concurrency exception and whole transaction is rolled back = no record is updated.

    You can still overcome the issue by passing list of modified records to the service (reducing roundtrips between client and service is a best practice) but you can process each record separately by calling SaveChanges for every single record. Anyway this should be very carefully considered because each call to SaveChanges is like separate unit of work – is it really what you want?

    Btw. the best practice is to make your service statless. You should avoid maintaining data between service calls and this example really doesn’t need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the JQuery .post method to get data using an AJAX call.
Going slightly crazy here. I'm making an Ajax call using jQuery.post as follows: $.post('php/print.php',{data:dS},function(res){...
According to this post , i ask $title. Which are your favorite PHP coding
According to this post on Daring Fireball a new iPhone SDK Agreement release in
according to this image, i try to save data in NSUserDefaults but my App
I am using urllib2 to post data to a form. The problem is that
According to this post: How to write a scalable Tcp/Ip based server jerrylvl states:
My Server returns: {content:<div>\n <form style=\width: auto\ id=\pictureUploadForm\ method=\POST\ enctype=\multipart/form-data\ action=\https://localhost/simmapp/action/admin.CMSAction\>\n <label for=\fileUpload\>Choose File
I've tried to include as much info in this post as possible. I'm using
According to this answer , it is better to use the $.ajax() function: $('.showprayer').click(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.