getchanges no generic

4 posts, 0 answers
  1. Andreas
    Andreas avatar
    2 posts
    Member since:
    Feb 2013

    Posted 13 Feb 2013 Link to this post

    Hi,

    by calling SaveChanges, I would like to log the according objects.

    ContextChanges contextChanges = dataContext.GetChanges();  

    IList<Foo> inserts = contextChanges.GetInserts<Foo>();

    gives me only the information for known objects.

    How can I enumerate all objects, independent from the class.
    I need something like that:
    List<object> inserts = contextChanges.GetInserts();

  2. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 14 Feb 2013 Link to this post

    Hi, Andreas

    You almost found the correct answer by yourself. You can pass any  type to the generic GetInserts method and the result will be a collection of all types that are descendants of the type you passed. 

    IList<object> allInserts = contextChanges.GetInserts<object>(); 

    This behaviour is described in the intellisense of the GetInserts method:




    By the way the same applies to the GetUpdates and GetDeletes methods as well.

    If you have any other questions, please feel free to ask.

    Regards,
    Yordan
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Andreas
    Andreas avatar
    2 posts
    Member since:
    Feb 2013

    Posted 15 Feb 2013 Link to this post

    Hi Yordan,

    thank you for answering.

    IList<
    object> allInserts = contextChanges.GetInserts<object>();


    When I use it nothing returns, except replacing "object" with the right class (Foo).

    IList<object
    > allInserts = contextChanges.GetInserts();
    would be nice, especially for debugging.

    Best regards

    Andreas



  5. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 15 Feb 2013 Link to this post

    Hello Andreas,

     
    The method 

    contextChanges.GetInserts<object>();
    should work exactly as the method you need - 
    contextChanges.GetInserts();

    You have to call it before calling 
    context.SaveChanges();

    For your convenience, here is a code snippet that demonstrates the outcomes:

    static void Main(string[] args)
            {
                using (EntitiesModel12 context = new EntitiesModel12())
                {
                    Employee employee = new Employee() { Company = "AW" };
                    Product product = new Product() { ProductName = "PN" };
                    context.Add(employee);
                    context.Add(product);
     
                    //context.SaveChanges(); if you call SaveCahanges() now insertedEmployees and allInserts collections will be empty later
     
                    ContextChanges contextChanges = context.GetChanges();
                    IList<Employee> insertedEmployees = contextChanges.GetInserts<Employee>(); //insertedEmployees.Count = 1
                    IList<object> allInserts = contextChanges.GetInserts<object>(); //allInserts.Count = 2;
                }
            }

    In case the method GetInserts<object>() still returns empty collection, let us know and we will convert the thread into private ticket conversation so that you can send us the project and we can analyze the possible reasons.



    Greetings,
    Yordan
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
Back to Top