This is a migrated thread and some comments may be shown as answers.

getchanges no generic

3 Answers 64 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andreas
Top achievements
Rank 1
Andreas asked on 13 Feb 2013, 07:36 AM
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();

3 Answers, 1 is accepted

Sort by
0
Yordan
Telerik team
answered on 14 Feb 2013, 08:57 AM
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.
0
Andreas
Top achievements
Rank 1
answered on 15 Feb 2013, 06:10 AM
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



0
Yordan
Telerik team
answered on 15 Feb 2013, 10:55 AM
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.
Tags
Getting Started
Asked by
Andreas
Top achievements
Rank 1
Answers by
Yordan
Telerik team
Andreas
Top achievements
Rank 1
Share this question
or