你可以用
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>
. 据我所知,您应该知道客户本身是否发生了变化,以及是否有来自该客户的订单也发生了变化。