none
Self-Tracking Entities: Original Values and Update Customization - Update ComplexType failed RRS feed

  • Question

  • Hi @ll,

    I think some of us know the blog entry by Jeff Derstadt (http://blogs.msdn.com/b/adonet/archive/2011/02/09/self-tracking-entities-original-values-and-update-customization.aspx). I followed his second approach by "Update only Modified Properties" but I have a problem with it. If I change a ComplexType I don`t get an update. If I change a normal property I get an update.I am using Self-Tracking-Entities wit Entity Framework 4.0, Model First.

     

    Any help would be nice :-)

     

    Kind Regards,

    Daniel

    Wednesday, July 13, 2011 12:51 PM

All replies

  • Push again!

     

    Have anybody the same problem?

     

    Kind Regards,

    Daniel

    Thursday, July 14, 2011 11:23 AM
  • Hi Dani

     

    Welcome to the forum.

    When we update the property, it will fired the OnPropertyChanged event to change the state.

    The realize:

    Protected virtual void OnPropertyChanged(String propertyName)

    {

    If(ChangeTrack.State!=Object.Added&&ChangeTrack.State!=ObjectState.Deleted)

    {

        ChangeTrack.State=ObjectState.Modified;

    }

    }

    The ObjectState realize:

    Public ObjectState State

    {

           Get…

         Set{

           If(_isDeserializing||_changeTracingEnabled)// both are private and false;

          {

               。。。

              _objectState=value;

         }

       }

    }

     

    When we have the ComplexType refactored by the Entity the state will be updated.

    Once the ComplexType is created by ourselves, the state is Added and can’t  changed manually.

    I hope this would be helpful.

     


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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, July 15, 2011 10:07 AM
    Moderator
  • Hi David,

     

    I think you understand me wrong. What I mean was I dont get an update to the database when I change a complex property. No SQL update was send to the database.

     

     

    Person First = Persons.First(); //List of persons. They are allready in the database.
    First.AcceptChanges();
    First.ContactInformation.Name = "Test";
    
    using (MyModelContainer Container = new MyModelContainer())
    {
      Container.ApplyChanges("PersonSet", First);
      Container.SaveChanges();
    
    }
    

     

    Kind Regards,

    Daniel

     

     


    Friday, July 15, 2011 2:28 PM
  • Sorry, but I musst push again!

     

    Have anybody the same problem? Is there nobody who have the same problem with this?

     

    Kind Regards,

    Daniel

    Monday, July 18, 2011 2:16 PM
  • Hi Dani,

     

    I'm sorry that reply so late. I test that once the complex was refactor by the Entity, we can update it. Could you show me

    an sample code? So that I could have a more  in-depth study.



    David Peng [MSFT]
    MSDN Community Support | 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, July 19, 2011 1:51 AM
    Moderator
  • Hi David,

    how can I give you an example? I changed my Model.tt and my Model.Context.tt as described in this http://blogs.msdn.com/b/adonet/archive/2011/02/09/self-tracking-entities-original-values-and-update-customization.aspx. I downloaded the t4 Samples from this website and tried this one but I don´t get an update when I change a complex property. My complex property is

    ContactInformation:

    + ZipCode : string

    + Street : string

     

    If i change the value of ZipCode to "bla bla bla" the value is set but the change tracker´s state is unchanged. After that I update my person with the changed contact information with this:

    using (MyModelContainer Container = new MyModelContainer())
    {
     Container.ApplyChanges("PersonSet", MyPerson); //MyPerson contains the contactinformation with the zipcode "bla bla bla"
     Container.SaveChanges();
    
    }
    

    No change was made to the database.

    Tuesday, July 19, 2011 7:52 AM
  • Hi Dani,

     

    You can use the self-track template that it's contained by the VS.

    You could show me a source code link ,  and then I could download it.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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.

    Thursday, July 21, 2011 8:40 AM
    Moderator
  • Hi David,

     

    I´m using the T4 Templates from Jeff Derstadt (http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-12-69-51/T4-Files.zip). I used this template not the default in VS for self-tracking-entites. I want to improve the performance for an update. When I use only normal properties (no complex property) it works well, but if I use a complex property like the one above (ContactInformation) nothing happens to the database.

     

    Kind Regards,

    Daniel

    Thursday, July 21, 2011 9:45 AM
  • Hi Dani,

     

    yes, I used the templates and I can change the complextype properties success.

    my code snippet:

    TestEntities ef=new TestEntities();

    var prod=ef.Product.First();

    prod.ComplexType.Title="Changet Complex";

    ef.savechanges();

     

     


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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, July 22, 2011 4:53 AM
    Moderator
  • Hi David,

     

    that´s really weird. I uploaded a prototype where I change the name of a person (normal property -> everything fine) and the complex property ContactInformation where I change the street value (no update was made to the database).

    The download link is here http://www35.zippyshare.com/v/56984289/file.html

    Sorry for the bad code but it is only a prototype ;-)

     

    Kind Regards,

    Dani

    Friday, July 22, 2011 7:56 AM
  • Hi Dani,

     

    Base your code sample, I suggest you could update the property with the same objectContext which is used to track the Entity's status.

    Once you have an new ObjectContext it will lose the Entity's status.

    My sample:

    Person

     

     

    PersonFromDatabase;

     

     

    using(MyUpdateModelContainer Container = new MyUpdateModelContainer()){

    PersonFromDatabase = Container.PersonSet.First();

     

    PersonFromDatabase.Name =

     

    "Dan*************iTest";

    PersonFromDatabase.ContactInformation.Street =

     

    "bli ************bla blubTest";

    Container.SaveChanges();

     

    }

    I hope that would be help you.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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, July 26, 2011 1:28 AM
    Moderator
  • Hi David,

    hmmm that helps me not really because I am building a Client/Server application with WCF 4.0 and Entity Framework 4.0. I can not do this in the same ObjectContex. The changes to the properties are all on the client side. I found it really weird because with normal properties everything worked... but with the complex properties nothing happens... :-(

     

    Kind Regards,

    Daniel

    Tuesday, July 26, 2011 7:19 AM
  • Hi Dani,

     

    No matter which ObjectContext you use,  once you want to update the entities you should attach it into the ObjectContext First.

    The download link is here http://www35.zippyshare.com/v/56984289/file.html

    Do the download link is refer to your sample code?  I could change the properties success.

    I think there are some states lost with your code. And It would be better if you could  show more details.

     

    Best Regards.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [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, July 26, 2011 8:28 AM
    Moderator
  • Hi David,

     

    hm how I should attach my entities to the context? The attach method is not possible because the default template doesn`t implement IEntityWithKey for the entities. I apply my changes to the context by calling ApplyChanges on the container with the changed person object. That attach my changed Person to the context.

    Container.ApplyChanges("PersonSet", MyPerson); 
    Container.SaveChanges();
    

    The application is a client/server application with wpf 4.0 -> wcf 4.0 -> entity framework 4.0. I don`t understand why you need more details because the sample code shows the usage on the server side and that´s all. The client doesn´t know anything about the entity framework only the self tracking entities are used on the client side. The changes are made on the client side and then send via wcf to the server. The server use the entity framework for the ms database.

     

    Kind Regards,

    Daniel


    Wednesday, July 27, 2011 8:19 AM