代码之家  ›  专栏  ›  技术社区  ›  Jeremy Cantrell

如何检查Linq to SQL EntitySet上的删除操作

  •  2
  • Jeremy Cantrell  · 技术社区  · 16 年前

    我有一个实体,它在一个实体集中有一个关联实体的集合。最后,我尝试报告一些对这个实体所做的更改。我很可能会使用getModifiedMembers()方法来执行此操作,我猜我可以对EntitySet中的每个实体执行相同的操作,但我不确定如何判断该EntitySet中是否存在任何删除操作。

    最好的方法是什么?

    2 回复  |  直到 15 年前
        1
  •  4
  •   bruno conde    16 年前

    你可以用 dataContext.GetChangeSet() 跟踪所有更改的实体并筛选特定的 T 实体。请查看这是否是您想要的:

        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());
            }
        }
    

    编辑:

    你需要这样的东西吗?

        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());
                }
            }
        }
    

    这个 Customer 是实体并具有 EntitySet<Order> . 据我所知,您应该知道客户本身是否发生了变化,以及是否有来自该客户的订单也发生了变化。

        2
  •  0
  •   Shannon Davidson    15 年前

    codesmith的plinqo捕获执行submitchanges时所做的所有更改,并将其打包到一个可以从上下文lastaudit属性访问的审核对象中。报告所做的更改及其更新类型。有一个样品在 http://plinqo.com/home.ashx?NoRedirect=1#Auditing_18