none
POCO With EF4.1 Adds Entity instead of updates RRS feed

  • Question

  • Hi,

     

    I have been trying to figure out this for hours now to no avail. I have a simple method that takes a POCO entity class called Applicant as a parameter and updates an existing entity to the value specified in the parameter. I am aware of the object graph not syncing in some cases but after stepping through the code.

    I've noticed that EF does track the changes when a property changes but somehow still manages to screw up the SaveChanges() call by inserting instead of updating the entity. This is very confusing as the Entity state right before the SaveChanges call is actually 'Modified'...this should generate an update right?.

    I am a little new to the POCO code-first approach so I've followed the article : http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx and created a DbContext class with the specified collection of my entities.

    My method that I call to update the Applicant Entity looks as follow:

    If Anyone can tell me why it adds an Applicant and not update, I would greatly appreciate it.

     

    Thanks.

    Mike

     

     

      public Applicant UpdateApplicant(Applicant applicant)
      {
       using (DatabaseContext context = new DatabaseContext())
       {
        try
        {
         context.Configuration.LazyLoadingEnabled = false;
         //Find checks whether the object is in the object stack first before hitting the back end
          
         Applicant applicantToEdit = context.Applicants.Find(applicant.Id); //State is unchanged here
         applicantToEdit.ApplicantType = applicant.ApplicantType;
         applicantToEdit.Name = applicant.Name; // State shows modified
    
         context.Entry(applicantToEdit).State = EntityState.Modified; //forcing it to modified? 
         int number = context.SaveChanges(); //Buggers up here and creates a new Applicant instead of updating an existing one
         return applicantToEdit;
        }
        catch (UpdateException updateException)
        {
         return null;
        }
       }
      }
    

     


    **UPDATE**

    I have notived that whenever I update a primitive-type property on the entity, it does actually do an update and not an insert but the moment I make a change on a property that is a Navigation Property, it does an insert. I will begin to investigate the relationship fix-up/tracking to see whether I can't solve this and come back. Below is my Entity

    [DataContract]
    public class Applicant : IApplicant
    { 
    
       [DataMember]
        public int Id { get; set; }
    
        [DataMember]
        public byte ApplicantTypeId { get; set; }
    
        [DataMember]
        public virtual IApplicantType ApplicantType { get; set; }
        
        [DataMember]
        public string Name { get; set; }
        
        [DataMember]
        public virtual List<IApplicantContactDetail> ApplicantContactDetails { get; set; }
        
        [DataMember]
        public virtual List<IApplicantAddress> ApplicantAddresses { get; set; }
    
        [DataMember]
        public virtual List<IApplication> Applications { get; set; }
    
    }

    • Edited by Pikker1981 Tuesday, August 2, 2011 5:43 AM Further details found
    Monday, August 1, 2011 10:10 PM

Answers

  • Hi Pikker,

    Welcome!

    >>> context.Entry(applicantToEdit).State = EntityState.Modified; //forcing it to modified?
    I think you can remove this code, when you change the entity's properties, the state has been set as Modified.---Just call SaveChanges()

    I'm not clear about your updated question, it seems relate to WCF Data Service. EF doesn't support Interface very well.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 2, 2011 3:43 PM
    Moderator

All replies

  • Hi Pikker,

    Welcome!

    >>> context.Entry(applicantToEdit).State = EntityState.Modified; //forcing it to modified?
    I think you can remove this code, when you change the entity's properties, the state has been set as Modified.---Just call SaveChanges()

    I'm not clear about your updated question, it seems relate to WCF Data Service. EF doesn't support Interface very well.

    Have a nice day.

     


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, August 2, 2011 3:43 PM
    Moderator
  • Hi Pikker,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance. 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 5, 2011 8:35 AM
    Moderator