2

Here's what I'd like to do:

var myCustomer = new Customer();
myCustomer.Name = "Bob";
myCustomer.HasAJob = true;
myCustomer.LikesPonies = false;

Then I'd like to pass it into an update method:

public UpdateCustomer(Customer cust)
{
  using(var context = dbcontext())
  {
     var dbCust = context.Customers.FirstOrDefault(c => c.Name == cust.Name);
     if(dbCust != null)
     {
        // Apply values from cust here so I don't have to do this:
        dbCust.HasAJob = cust.HasAJob;
        dbCust.LikesPonies = cust.LikesPonies
     }
     context.SaveChanges();
  }
}

The reason for this is I'm working in multiple different parts of my application, and/or across DLLs. Is this possible?

EDIT: Found this question to be immensely useful: Update Row if it Exists Else Insert Logic with Entity Framework

Community
  • 1
  • 1
John Zumbrum
  • 2,576
  • 8
  • 33
  • 58
  • Have you tried that block of code? What exactly is the question? Are you not happy with the way you're doing it now? Or is it not working? – JofryHS Nov 28 '12 at 23:14
  • I don't want to have to copy every property. That's painful for me to look at, write, and maintain. I want a "copy-all-properties" method. Does such a thing exist? – John Zumbrum Nov 28 '12 at 23:16
  • What about the key? If you know the key and it is set you could just Attach the entity and mark it as modified. – Pawel Nov 28 '12 at 23:18
  • @Pawel that sounds like something I've read before, do you have an example, make it an answer to this question? – John Zumbrum Nov 28 '12 at 23:20
  • It might be possible to do through reflection... But I'm in the same boat. I've been manually copying over stacks and stacks of variables, even when my object's fields match the db object's fields perfectly. – MadHenchbot Nov 28 '12 at 23:23
  • @MadHenchbot yea, this seems like a HUGE potential savings as that whole process seems horribly inefficient and prone to error – John Zumbrum Nov 28 '12 at 23:25

1 Answers1

1

If you are sure that the entity is in the database and you have key you would just Attach the object you have to the context. Note that attached entities are by default in Unchanged state as the assumption is that all the values of properties are the same as in the database. If this is not the case (i.e. values are different) you need to change the state of the entity to modified. Take a look at this blog post: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx it describes several sceanrios including the one you are asking about.

Pawel
  • 30,245
  • 4
  • 68
  • 99
  • 1
    I was reading this and it talks about using `context.Entity(someEntity).State = EntityState.Modified`, however I cannot resolve `Entity`. – The Muffin Man Nov 28 '12 at 23:35
  • It's actually context.Entry() and I don't have access to it either. I assume it is version-specific to EF 4.1. I found this SO link that might help: http://stackoverflow.com/questions/5667166/updating-my-ef-model-to-use-4-1-when-i-built-it-in-4-0/5667947#5667947 – MadHenchbot Nov 28 '12 at 23:42
  • 1
    ah there it is, thanks. `context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);` – The Muffin Man Nov 28 '12 at 23:44
  • 1
    @Nick: Are you using CodeFirst and DbContext or you are using ObjectContext? This might be the reason why you see the difference but the general idea is the same. – Pawel Nov 28 '12 at 23:46
  • Getting this error: The ObjectStateManager does not contain an ObjectStateEntry with a reference to an object of type 'Customer'. – John Zumbrum Nov 29 '12 at 21:33