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;
        }
    }
sstan
  • 35,425
  • 6
  • 48
  • 66
Adam Reed
  • 123
  • 6
  • I don't think it's possible without using some third party library. – sstan Aug 08 '16 at 18:05
  • You can have a look at: [Entity Framework Extended Library](https://github.com/loresoft/EntityFramework.Extended). 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. – sstan Aug 08 '16 at 18:13
  • Duplicate: http://stackoverflow.com/questions/4218566/update-a-record-without-first-querying – Steve Greene Aug 08 '16 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` – mschr Oct 29 '16 at 04:01

1 Answers1

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.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • 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? – Adam Reed Aug 08 '16 at 22:29
  • @AdamReed It is, but typically it involves overriding a little bit of logic and changing entity state for those properties, as outlined here: http://codereview.stackexchange.com/questions/37304/update-only-modified-fields-in-entity-framework 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. – Dillie-O Aug 08 '16 at 22:36
  • 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. – Adam Reed Aug 08 '16 at 22:38
  • @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: http://stackoverflow.com/questions/10257360/how-to-update-not-every-fields-of-an-object-using-entity-framework-and-entitysta – Dillie-O Aug 08 '16 at 22:40
  • 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 – Adam Reed Aug 08 '16 at 22:44
  • @AdamReed Well, one thing you can try is attaching the object, which based on the previous code would attach the object in an "unmodified" state. Then, instead of marking the entire entity as modified, you could mark just the one property as modified, and see if that does the trick for you. – Dillie-O Aug 08 '16 at 22:52
  • Thanks I'll give that a shot – Adam Reed Aug 08 '16 at 22:52