none
how can i get a list of modification that happend in entity navigation property (add or removed) RRS feed

  • Question

  • hi

    i have project with 2 entities , Student and ClassRoom

    there is many to many relation between Student and ClassRoom,

    there is a navigation property Student.ClassRooms and ClassRoom.Students

    if the user modified the selected Student ClassRooms , add or removed ones , how can i find ,generate ,list of changes in Student ClassRooms ? (find the added ClassRoomand removed ones )

    there is a way to fins if there is a added or modified Student :

    dc.ChangeTracker.Entries<Student >().Any(eee => eee.State == EntityState.Added)
          

    id there a similar one for navigation property ?       


    Wednesday, October 9, 2013 9:14 AM

Answers

  • >>how EF keep track of changes in navigation property in many to many relation?

    In my opinion, entity framework supports entity state changes as Added, Modified and so on, however, what you want is a log that records an entity like where it comes from, where it will go. I think entity framework does not support this.

    For achieving record the relationship, there is link regarding it:

    http://stackoverflow.com/questions/7490509/entity-framework-4-1-many-to-many-relationships-change-tracking

    In the link, it writes an extended function to implement the issue.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Hani Safa Friday, October 11, 2013 8:53 PM
    Friday, October 11, 2013 9:49 AM
    Moderator

All replies

  • Hello,

    If I understand correctly, you want collect the added or removed navigation property.

    For achieving this, I made a sample and please see it below:

    using (S09Container db = new S09Container())
    
                {
    
                    Class cl = new Class() { Title = "0", Duration = new TimeSpan(1, 1, 1) };
    
    
                    db.Classes.Add(cl);
    
    
                    db.Classes.ToList();
    
    
                    List<Class> classesList = new List<Class>();
    
    
                    foreach (DbEntityEntry<Class> c in db.ChangeTracker.Entries<Class>()
    
                        .Where(c => c.Entity.Students.Where(s => s.Id == 1).Count() > 0
    
                            & (c.State == EntityState.Added || c.State == EntityState.Deleted)))
    
                    {
    
                        classesList.Add(c.Entity);
    
                    }
    
                }
    

    In the sample, the class and the student are many to many relationship and it will collect classes which has studentID = 1 and its state is added or removed.

    If this does not work for you, please let me know.  

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 10, 2013 3:13 AM
    Moderator
  • Hi Hani;

    The code snippet below will get the change set collection of all object that will be updated to the database.

    // New up a data context
    var db = new ContextEntities();
    
    //  Do your add modify and deletes
    
    // To get a collection of those entities that have been added, modified or deleted
    // you can run the following code. This is assumeing you are using DbContext. If
    // you are using ObjectContext then skip the first line which gets a Object context
    // from DbContext.
    var objectContext = ((IObjectContextAdapter) db).ObjectContext;
    var changeSet = objectContext.ObjectStateManager.GetObjectStateEntries(
        EntityState.Modified | EntityState.Added | EntityState.Deleted);
    
    foreach (ObjectStateEntry objectStateEntry in changeSet)
    {
    	  // Each ObjectStateEntry has all the info you want to know.
        Console.WriteLine(objectStateEntry....);
    }
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, October 10, 2013 4:07 AM
  • thanks a lot for your reply ,

    yes i need the added or removed navigation property , but i did not explained the question correctly ,

    suppose you have Math , Sport Class and Fred, Hani ,Jim Students.

    Math class has Fred , Hani.

    Sport Class has no student.

    the user run the application an remove Hani from math class And/OR add Fred,Jim to sport class and clicked a button.

    how can i know what the user did?

    there is no modification to the entity , just the relation between the entities, if you will.

    I've found an answer but I'm not sure its the best one ,

    for each class , get the student list from the DB (fresh ,not modified copy) , get the edit classes student (what the user did) and compare them to find what happened 

    Thursday, October 10, 2013 7:14 PM
  • Hi Fernando,

    thanks for your reply ,

    what i need is not the modification in the entity  , but the modification in the relation between the entities.

    if you have a many to many relation between student and classes , and you add an existing student (S1) to an existing class(C1) , how can i find that class C1 now have Student S1 ?

    one more thing , i'm using EF 5 DbContext's and its  ChangeTracker Not objectContext.ObjectStateManager

    Thursday, October 10, 2013 7:22 PM
  • Hi Hani Safa,

    >>how can i know what the user did?

    So what you want is that you want know the entities change logs.

    For this, my suggestion is that we can add a log table that to record the changes of the entities.

    When you want to see who has done what thing at when, you just need to have a look at the log table.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 11, 2013 3:41 AM
    Moderator
  • Hi Fred Bao,

    that will do to. but,

    ChangeTracker keep track of entity , it has snap shot of entity property so if i changed the name of one of the Classes (Sport make it  Sportssss , for instance ) and run

    var cla = dc.ClassSet.Where(c => c.Id == 2).Select(c => c).FirstOrDefault();    
    cla.Name = "Sportsssss";
    if (dc.ChangeTracker.Entries<Class>().Any(eee => eee.State == EntityState.Modified))
    {
        // Do something
    }    
    

    the result if the if statement will be true , there are modification , BUT

    for the same calss instance (sport) , i've added students

    var cla = dc.ClassSet.Where(c => c.Id == 2).Select(c => c).FirstOrDefault();    
    var student_1 = dc.StudentSet.Where(s=> s.Id == 1).Select(s=>s).FirstOrDefault();
    cla.Students.Add(student_1);
    
    if (dc.ChangeTracker.Entries<Class>().Any(eee => eee.State == EntityState.Modified))
    {
        // Do Something
    }    

    the result of the if statement if false , it did not detect that change ,

    how EF keep track of changes in navigation property in many to many relation ?

    eventually , it will add a record in a hidden table (StudentClasses) (not visible in the designer ), how can we detect it ?


    Friday, October 11, 2013 7:48 AM
  • >>how EF keep track of changes in navigation property in many to many relation?

    In my opinion, entity framework supports entity state changes as Added, Modified and so on, however, what you want is a log that records an entity like where it comes from, where it will go. I think entity framework does not support this.

    For achieving record the relationship, there is link regarding it:

    http://stackoverflow.com/questions/7490509/entity-framework-4-1-many-to-many-relationships-change-tracking

    In the link, it writes an extended function to implement the issue.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Hani Safa Friday, October 11, 2013 8:53 PM
    Friday, October 11, 2013 9:49 AM
    Moderator