tags:

views:

763

answers:

2

I have an entity that has a collection of associated entities in an EntitySet. Ultimately, I'm trying to report on some changes that have been made to this entity. I will most likely use the GetModifiedMembers() method to do this, and I'm guessing I can just do the same with each entity in the EntitySet, but I'm not sure how to tell if there have been any deletions in that EntitySet.

What's the best way to do this?

+3  A: 

You could use the dataContext.GetChangeSet() to track all the changed entities and filter the specific T entities. Please see if this is what you want:

    public static void ShowModifiedEntitiesInfo<T>(DataContext context) where T : class
    {
        foreach (var entity in context.GetChangeSet().Updates.Where(del => del is T).Cast<T>())
        {
            ModifiedMemberInfo[] modifiedMembers = context.GetTable<T>().GetModifiedMembers(entity);
            Console.WriteLine("Updated Entity: " + entity.ToString());
            Console.WriteLine("     (Members Changed)");
            foreach (var member in modifiedMembers)
            {
                Console.WriteLine("     - Member Name: " + member.Member.Name);
                Console.WriteLine("     - Original Value: " + member.OriginalValue.ToString());
                Console.WriteLine("     - Current Value: " + member.CurrentValue.ToString());
            }
        }
        foreach (var entity in context.GetChangeSet().Inserts.Where(del => del is T).Cast<T>())
        {
            Console.WriteLine("Inserted Entity: " + entity.ToString());
        }
        foreach (var entity in context.GetChangeSet().Deletes.Where(del => del is T).Cast<T>())
        {
            Console.WriteLine("Deleted Entity: " + entity.ToString());
        }
    }

EDIT:

Is what you need something like this?

    public static void ShowModifiedCustomerInfo(MyDataContext context, Customer customer)
    {
        ModifiedMemberInfo[] modifiedMembers = context.Customers.GetModifiedMembers(customer);

        List<Order> updatedOrders = context.GetChangeSet().Updates.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();

        List<Order> insertedOrders = context.GetChangeSet().Inserts.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();

        List<Order> deletedOrders = context.GetChangeSet().Deletes.Where(e => e is Order).Cast<Order>().Intersect<Order>(customer.Orders).ToList<Order>();

        if (modifiedMembers.Length > 0 || updatedOrders.Count > 0 || insertedOrders.Count > 0 || deletedOrders.Count > 0)
        {
            Console.WriteLine("Updated Customer: " + customer.ToString());

            foreach (var member in modifiedMembers)
            {
                Console.WriteLine("     - Member Name: " + member.Member.Name);
                Console.WriteLine("     - Original Value: " + member.OriginalValue.ToString());
                Console.WriteLine("     - Current Value: " + member.CurrentValue.ToString());
            }

            foreach (var entity in updatedOrders)
            {
                Console.WriteLine("     Updated Order: " + entity.ToString());
            }

            foreach (var entity in insertedOrders)
            {
                Console.WriteLine("     Inserted Order: " + entity.ToString());
            }

            foreach (var entity in deletedOrders)
            {
                Console.WriteLine("     Deleted Order: " + entity.ToString());
            }
        }
    }

The Customer is the entity and has an EntitySet<Order>. For what I understand, you what to know if the customer itself has changed and if there was any order from this customer that also changed.

bruno conde
I thought about using GetChangeSet(), but how would I know if one of the deleted entities was a member of the parent's EntitySet?
Jeremy Cantrell
Edited the answer ...
bruno conde
I tried this, but it doesn't seem to work. I think it may have something to do with how I'm loading the child entities. I'm marking your answer as correct though, because I think the problem is on my side.
Jeremy Cantrell
A: 

CodeSmith's PLINQO captures all changes made when SubmitChanges is executed and packages it all up in an Audit object that can be accessed from the context LastAudit property. Reports on what changed and what type of update it was. There is a sample at http://plinqo.com/home.ashx?NoRedirect=1#Auditing_18

Shannon Davidson