0

Is it possible to update objects with Entity Framework, without grabbing them first?

Example: Here, I have a function that provides a Primary Key to locate the objects, pulls them, then updates them. I would like to eliminate having to pull the objects first, and simply run an UPDATE query. Removing the need for the SELECT query being generated.

    public async Task<int> UpdateChecks(long? acctId, string payorname, string checkaccountnumber, string checkroutingnumber, string checkaccounttype)
    {
        using (var max = new Max(_max.ConnectionString))
        {
            var payments = await
                max.payments.Where(
                    w =>
                        w.maindatabaseid == acctId && (w.paymentstatus == "PENDING" || w.paymentstatus == "HOLD")).ToListAsync();

            payments.AsParallel().ForAll(payment =>
            {
                payment.payorname = payorname;
                payment.checkaccountnumber = checkaccountnumber;
                payment.checkroutingnumber = checkroutingnumber;
                payment.checkaccounttype = checkaccounttype;
                payment.paymentmethod = "CHECK";
                payment.paymentstatus = "HOLD";
            });

            await max.SaveChangesAsync();
            return payments.Count;
        }
    }
4
  • I don't think it's possible without using some third party library. Commented Aug 8, 2016 at 18:05
  • You can have a look at: Entity Framework Extended Library. According to their page, it does exactly what you're looking for. But I've never tried myself with the latest versions of EF6, so I don't know how well it works. Commented Aug 8, 2016 at 18:13
  • Duplicate: stackoverflow.com/questions/4218566/… Commented Aug 8, 2016 at 18:41
  • Im not entirely sure, but i believe that if you do not call ToList - you will avoid having to load entities into memory. Instead iterate over the IQuerable as opposed to IEnumerable Commented Oct 29, 2016 at 4:01

1 Answer 1

0

You can use the Attach() command to attach an entity you already know exists and then call SaveChanges() will will call the appropriate update method. Here is some sample code from the MSDN article on the topic:

on the subject:

var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" }; 

using (var context = new BloggingContext()) 
{ 
    context.Entry(existingBlog).State = EntityState.Unchanged; 

    // Do some more work...  

    context.SaveChanges(); 
}

Note that this is general EF logic, not related to any specific database implementation.

Sign up to request clarification or add additional context in comments.

7 Comments

Is it possible to only provide such thing as the primary key in the mock object and then set a specific fields value and have it only update that fields value?
@AdamReed It is, but typically it involves overriding a little bit of logic and changing entity state for those properties, as outlined here: codereview.stackexchange.com/questions/37304/… However, as mentioned in the comments, you often don't have to worry about that unless there are reasons to only modify those specific fields.
A database I'm working with has well over 200 fields (unfortunately) and I constantly have to update just a single field. If I only provide the object primary key in the object I attach and update a single field and set that field state will it only update that specific field then? Or do I have to set every other field to unchanged.
@AdamReed So for something like that, if you want to pass in the property being modified as well, you can set just the field involved as modified and that should make your update statement update just the specified field, as outlined here: stackoverflow.com/questions/10257360/…
Those implementations seem quite ugly to me. Am I able to set the entity to unchanged then only that field to modified? Their examples seem to describe the opposite where they set the entity to modified then the fields to unchanged :(. 0
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.