none
problem: entity framework reverting changed property value befor savechanges() RRS feed

  • Question

  • Hi,

    I am using entity framework to add, update, delete data.

    I have created an object of Address entity (addressObj), also I get the address object (tempAddress) from dbContext based on a where condition.

    To copy all property value of (tempAddress) to (addressObj) i used a function as bellow:

     Utility.CopyPropertyValues(tempAddress, addressObj);

            public static void CopyPropertyValues(object source, object destination)
            {
                var destProperties = destination.GetType().GetProperties();
    
                foreach (var sourceProperty in source.GetType().GetProperties())
                {
                    foreach (var destProperty in destProperties)
                    {
                        if (destProperty.Name == sourceProperty.Name &&
                    destProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
                        {
                            destProperty.SetValue(destination, sourceProperty.GetValue(
                                source, new object[] { }), new object[] { });
    
                            break;
                        }
                    }
                }
            }


    Now I need to modify few property value to add a new record to address table (complete code bellow:)

      var tempAddress = db.LML_Address.Where(address => address.OrderHeaderID == QItem.OrderID).FirstOrDefault();
    
                                var addressObj = new LML_Address();
                                
                                Utility.CopyPropertyValues(tempAddress, addressObj);
    
                                var addId = Guid.NewGuid();
                                addressObj.AddressID = addId;
                                // order header id of new refill order
                                addressObj.OrderHeaderID = objectOH.OrderHeaderID;
    
                                addressObj.OrderFulfilmentHeaders = new Collection<OrderFulfilmentHeader>();
    
                                var objectADDRESS = db.LML_Address.Add(addressObj);
                                db.SaveChanges();

    Now problem is at second last line (

    var objectADDRESS = db.LML_Address.Add(addressObj);

    )

    PROBLEM:

    as soon as we move from this line to (db.SaveChanges()); immediately I noticed that value of just modified property (addressObj.OrderHeaderId) changed to its previous value.

    Please help me out, why is this strange behaviour?

    Thanks.



    Wednesday, August 14, 2013 1:54 PM

Answers

  • Thanks for reply,

    LML_Address table was having few foreign keys, & for each foreign key, entity framework generates additional  property which contain complete object corresponding to that foreign key.

    Don't know why, BUT, When I made those object to 'null' things worked as expected. below are the additional lines that added to make few properties 'null'

    .

    .

    .

    . addressObj.OrderFulfilmentHeaders = new Collection<OrderFulfilmentHeader>(); //<<<<<<<<<< make properties representing tables corresponding to foreign key, to null addressObj.LML_AddressType = null; addressObj.LML_Countries = null; addressObj.LML_CustomerDetails = null; addressObj.LML_OrderHeader = null; var objectADDRESS = db.LML_Address.Add(addressObj); db.SaveChanges();


    Thanks, Got solved with this.

    • Marked as answer by Fusion Coder Friday, August 16, 2013 1:22 PM
    Friday, August 16, 2013 1:22 PM

All replies

  • Hi Fusion,

    Thank you for posting in our MSDN Community.

    >as soon as we move from this line to (db.SaveChanges()); immediately I noticed that value of just modified property (addressObj.OrderHeaderId) changed to its previous value.

    Please help me out, why is this strange behavior?

    This isn’t a strange behavior. Because the “Address” class is under the control of Entity Framework. The Entity will help you to manager the primary key, foreign key and those relationships.

    Let’s do a simple test.

     using (var db = new AddressContext())
                {
                    var newAddress = new Address();
                    Console.WriteLine(newAddress.AddressID);
                    db.LML_Address.Add(newAddress);
                    db.SaveChanges();
                    Console.WriteLine(newAddress.AddressID);
                }
     

    I generated an instance of “Address” class, but not set the value of the instance. The framework will help us to set default value to 0. I add the instance to Entity then call the “SaveChanges” method. The Entity will translate the instance to SQL statement then insert into table. When the Entity return, you could see the value of the “AddressID” property is change to 9. Because SqlServer has completed the insert into statement, the value in table was 9.

    I’d like to refer you to our MSDN reference below to know Entity States and SaveChanges it works.

    http://msdn.microsoft.com/en-us/data/jj592676.aspx

    Best Regards,


    Thursday, August 15, 2013 4:21 AM
    Moderator
  • Thanks for reply,

    LML_Address table was having few foreign keys, & for each foreign key, entity framework generates additional  property which contain complete object corresponding to that foreign key.

    Don't know why, BUT, When I made those object to 'null' things worked as expected. below are the additional lines that added to make few properties 'null'

    .

    .

    .

    . addressObj.OrderFulfilmentHeaders = new Collection<OrderFulfilmentHeader>(); //<<<<<<<<<< make properties representing tables corresponding to foreign key, to null addressObj.LML_AddressType = null; addressObj.LML_Countries = null; addressObj.LML_CustomerDetails = null; addressObj.LML_OrderHeader = null; var objectADDRESS = db.LML_Address.Add(addressObj); db.SaveChanges();


    Thanks, Got solved with this.

    • Marked as answer by Fusion Coder Friday, August 16, 2013 1:22 PM
    Friday, August 16, 2013 1:22 PM