本文整理汇总了C#中ChangeTrackingCollection.GetChanges方法的典型用法代码示例。如果您正苦于以下问题:C# ChangeTrackingCollection.GetChanges方法的具体用法?C# ChangeTrackingCollection.GetChanges怎么用?C# ChangeTrackingCollection.GetChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ChangeTrackingCollection
的用法示例。
在下文中一共展示了ChangeTrackingCollection.GetChanges方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetChanges_On_Existing_Order_With_Modified_Customer_Should_Return_Customer_Marked_As_Modified
public void GetChanges_On_Existing_Order_With_Modified_Customer_Should_Return_Customer_Marked_As_Modified()
{
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(order);
order.Customer.CustomerName = "xxx";
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.NotEmpty(changes);
Assert.Equal(TrackingState.Unchanged, changes.First().TrackingState);
Assert.Equal(TrackingState.Modified, changes.First().Customer.TrackingState);
}
示例2: Main
static void Main(string[] args)
{
Console.WriteLine("Press Enter to start");
Console.ReadLine();
ICustomerService customerService = new ChannelFactory<ICustomerService>("customerService").CreateChannel();
IOrderService orderService = new ChannelFactory<IOrderService>("orderService").CreateChannel();
using ((IDisposable)customerService)
using ((IDisposable)orderService)
{
try
{
// Get customers
Console.WriteLine("Customers:");
IEnumerable<Customer> customers = customerService.GetCustomersAsync().Result;
if (customers == null) return;
foreach (var c in customers)
PrintCustomer(c);
// Get orders for a customer
Console.WriteLine("\nGet customer orders {CustomerId}:");
string customerId = Console.ReadLine();
if (!customers.Any(c => string.Equals(c.CustomerId, customerId, StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("Invalid customer id: {0}", customerId.ToUpper());
return;
}
IEnumerable<Order> orders = orderService.GetCustomerOrdersAsync(customerId).Result;
foreach (var o in orders)
PrintOrder(o);
// Get an order
Console.WriteLine("\nGet an order {OrderId}:");
int orderId = int.Parse(Console.ReadLine());
if (!orders.Any(o => o.OrderId == orderId))
{
Console.WriteLine("Invalid order id: {0}", orderId);
return;
}
Order order = orderService.GetOrderAsync(orderId).Result;
PrintOrderWithDetails(order);
// Create a new order
Console.WriteLine("\nPress Enter to create a new order for {0}",
customerId.ToUpper());
Console.ReadLine();
var newOrder = new Order
{
CustomerId = customerId,
OrderDate = DateTime.Today,
ShippedDate = DateTime.Today.AddDays(1),
OrderDetails = new ChangeTrackingCollection<OrderDetail>
{
new OrderDetail { ProductId = 1, Quantity = 5, UnitPrice = 10 },
new OrderDetail { ProductId = 2, Quantity = 10, UnitPrice = 20 },
new OrderDetail { ProductId = 4, Quantity = 40, UnitPrice = 40 }
}
};
var createdOrder = orderService.CreateOrderAsync(newOrder).Result;
PrintOrderWithDetails(createdOrder);
// Update the order
Console.WriteLine("\nPress Enter to update order details");
Console.ReadLine();
// Start change-tracking the order
var changeTracker = new ChangeTrackingCollection<Order>(createdOrder);
// Modify order details
createdOrder.OrderDetails[0].UnitPrice++;
createdOrder.OrderDetails.RemoveAt(1);
createdOrder.OrderDetails.Add(new OrderDetail
{
OrderId = createdOrder.OrderId,
ProductId = 3,
Quantity = 15,
UnitPrice = 30
});
// Submit changes
Order changedOrder = changeTracker.GetChanges().SingleOrDefault();
Order updatedOrder = orderService.UpdateOrderAsync(changedOrder).Result;
// Merge changes
changeTracker.MergeChanges(updatedOrder);
Console.WriteLine("Updated order:");
PrintOrderWithDetails(createdOrder);
// Delete the order
Console.WriteLine("\nPress Enter to delete the order");
Console.ReadLine();
bool deleted = orderService.DeleteOrderAsync(createdOrder.OrderId).Result;
// Verify order was deleted
Order deletedOrder = orderService.GetOrderAsync(createdOrder.OrderId).Result;
Console.WriteLine(deleted && deletedOrder == null
? "Order was successfully deleted"
: "Order was not deleted");
//.........这里部分代码省略.........
示例3: GetChanges_Should_Return_Deleted_Items
public void GetChanges_Should_Return_Deleted_Items()
{
// Arrange
var database = new MockNorthwind();
var product = database.Products[0];
var changeTracker = new ChangeTrackingCollection<Product>(product);
changeTracker.Remove(product);
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.AreEqual(TrackingState.Deleted, changes.First().TrackingState);
}
示例4: MergeChanges_Should_Set_Unchanged_Customer_With_Modified_Setting
public void MergeChanges_Should_Set_Unchanged_Customer_With_Modified_Setting()
{
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var customer = order.Customer;
customer.CustomerSetting = new CustomerSetting
{
CustomerId = customer.CustomerId,
Customer = customer,
Setting = "Setting1"
};
var changeTracker = new ChangeTrackingCollection<Order>(order);
var setting = customer.CustomerSetting.Setting = "xxx";
var origState = customer.CustomerSetting.TrackingState;
var changes = changeTracker.GetChanges();
var changedCustomer = changes[0].Customer;
var updatedOrder = changes[0].Clone<Order>();
updatedOrder.AcceptChanges();
// Act
changeTracker.MergeChanges(updatedOrder);
// Assert
Assert.Equal(setting, customer.CustomerSetting.Setting); // Changed customer setting is preserved
Assert.Equal(TrackingState.Modified, origState); // Changed state is modified
Assert.Equal(TrackingState.Unchanged, customer.TrackingState); // State set to unchanged
}
示例5: MergeChanges_Should_Merge_Updates_For_Unchanged_Employee_With_Changed_Territories
public void MergeChanges_Should_Merge_Updates_For_Unchanged_Employee_With_Changed_Territories()
{
// Arrange
var database = new MockNorthwind();
var employee = database.Employees[0];
var unchangedTerritory = employee.Territories[0];
var modifiedTerritory = employee.Territories[1];
var deletedTerritory = employee.Territories[2];
var addedExistingTerritory = database.Territories[3];
var area1 = new Area { AreaId = 1, AreaName = "Northern", Territories = new ChangeTrackingCollection<Territory> { unchangedTerritory } };
var area2 = new Area { AreaId = 2, AreaName = "Southern", Territories = new ChangeTrackingCollection<Territory> { modifiedTerritory } };
var area3 = new Area { AreaId = 3, AreaName = "Eastern", Territories = new ChangeTrackingCollection<Territory> { deletedTerritory } };
var area4 = new Area { AreaId = 4, AreaName = "Western", Territories = new ChangeTrackingCollection<Territory> { addedExistingTerritory } };
var addedNewTerritory = new Territory
{
TerritoryId = "91360",
TerritoryDescription = "SouthernCalifornia",
Employees = new ChangeTrackingCollection<Employee> { employee },
AreaId = 5,
TrackingState = TrackingState.Added // Must explicitly mark as added
};
unchangedTerritory.Area = area1;
unchangedTerritory.AreaId = area1.AreaId;
modifiedTerritory.Area = area2;
modifiedTerritory.AreaId = area2.AreaId;
deletedTerritory.Area = area3;
deletedTerritory.AreaId = area3.AreaId;
addedExistingTerritory.Area = area4;
addedExistingTerritory.AreaId = area4.AreaId;
var changeTracker = new ChangeTrackingCollection<Employee>(employee);
modifiedTerritory.TerritoryDescription = "xxx"; // mod prop
modifiedTerritory.AreaId = 6; // mod FK prop
employee.Territories.Remove(deletedTerritory); // should mark area deleted
employee.Territories.Add(addedExistingTerritory); // should keep area unchanged
employee.Territories.Add(addedNewTerritory); // should keep territory added
addedExistingTerritory.Area.AreaName = "zzz"; // should mark area modified
var changes = changeTracker.GetChanges(); // should exclude unchanged territory
var updatedEmployee = UpdateEmployeesWithTerritories(changes)[0]; // rel entities, db-gen vals
// Act
changeTracker.MergeChanges(updatedEmployee);
// Assert
Assert.Contains(unchangedTerritory, employee.Territories); // Unchanged present
Assert.Equal("zzz", addedExistingTerritory.Area.AreaName); // Area name updated
Assert.Equal("yyy", addedNewTerritory.Data); // Db-generated value set
Assert.Equal(addedNewTerritory.AreaId, addedNewTerritory.Area.AreaId); // Added territory Area set
Assert.Equal(modifiedTerritory.AreaId, modifiedTerritory.Area.AreaId); // Modified territory Area set
Assert.Same(addedExistingTerritory, employee.Territories.Single(t => t.TerritoryId == "02116")); // Ref equality
Assert.Same(addedNewTerritory, employee.Territories.Single(t => t.TerritoryId == "91360")); // Ref equality
Assert.Same(modifiedTerritory, employee.Territories.Single(t => t.TerritoryId == "01730")); // Ref equality
Assert.DoesNotContain(deletedTerritory, employee.Territories); // Detail deleted
ICollection cachedDeletes = ((ITrackingCollection)employee.Territories).GetChanges(true);
Assert.Empty(cachedDeletes); // Cached deletes have been removed
}
示例6: MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_Changed_OrderDetails
public void MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_Changed_OrderDetails()
{
// Arrange
var database = new MockNorthwind();
var origOrder = database.Orders[0];
var product = database.Products.Single(p => p.ProductId == 14);
origOrder.OrderDetails.Add(new OrderDetail { ProductId = 14, OrderId = 10249, Quantity = 9, UnitPrice = 18.6000M, Product = product });
var unchangedDetail = origOrder.OrderDetails[0];
var modifiedDetail = origOrder.OrderDetails[1];
var deletedDetail = origOrder.OrderDetails[3];
var changeTracker = new ChangeTrackingCollection<Order>(origOrder);
origOrder.OrderDetails[1].Product.ProductName = "xxx";
origOrder.OrderDetails[2].Quantity++;
origOrder.OrderDetails[2].ProductId = 1;
var newUnitPrice = origOrder.OrderDetails[2].UnitPrice + 1;
origOrder.OrderDetails.RemoveAt(3);
var addedDetail = new OrderDetail {ProductId = 51, OrderId = 10249, Quantity = 40, UnitPrice = 42.4000M};
origOrder.OrderDetails.Add(addedDetail);
var changes = changeTracker.GetChanges();
var updatedOrder = UpdateOrdersWithDetails(database, changes)[0];
// Act
changeTracker.MergeChanges(updatedOrder);
// Assert
Assert.Contains(unchangedDetail, origOrder.OrderDetails); // Unchanged present
Assert.Equal("xxx", origOrder.OrderDetails[1].Product.ProductName); // Prod name updated
Assert.Equal(updatedOrder.OrderDetails[1].ProductId, origOrder.OrderDetails[2].Product.ProductId); // Changed Product set
Assert.Equal(newUnitPrice, origOrder.OrderDetails[2].UnitPrice); // Db-generated value set
Assert.Equal(updatedOrder.OrderDetails[2].Product.ProductId, origOrder.OrderDetails[3].Product.ProductId); // Added detail Product set
Assert.True(origOrder.OrderDetails.All(d => d.TrackingState == TrackingState.Unchanged)); // Details unchanged
Assert.Same(addedDetail, origOrder.OrderDetails.Single(d => d.ProductId == 51)); // Ref equality
Assert.Same(modifiedDetail, origOrder.OrderDetails.Single(d => d.ProductId == 42)); // Ref equality
Assert.DoesNotContain(deletedDetail, origOrder.OrderDetails); // Detail deleted
ICollection cachedDeletes = ((ITrackingCollection)origOrder.OrderDetails).GetChanges(true);
Assert.Empty(cachedDeletes); // Cached deletes have been removed
}
示例7: MergeChanges_Should_Set_Order_With_Modified_Customer_With_Db_Generated_Values
public void MergeChanges_Should_Set_Order_With_Modified_Customer_With_Db_Generated_Values()
{
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(order);
var customer = order.Customer;
var name = customer.CustomerName = "xxx";
var changes = changeTracker.GetChanges();
var updatedOrder = changes[0].Clone<Order>();
var data = updatedOrder.Customer.Data = "yyy"; // simulate db-generated values
updatedOrder.AcceptChanges();
// Act
changeTracker.MergeChanges(updatedOrder);
// Assert
Assert.Equal(name, order.Customer.CustomerName);
Assert.Equal(data, order.Customer.Data);
}
示例8: MockNorthwind
public void GetChanges_On_Existing_Customer_With_Manually_Added_CustomerSetting_Return_CustomerSetting_Marked_As_Added()
{
// Arrange
var database = new MockNorthwind();
var customer = database.Customers[0];
var changeTracker = new ChangeTrackingCollection<Customer>(customer);
customer.CustomerSetting = new CustomerSetting
{
CustomerId = customer.CustomerId,
Setting = "Setting1",
Customer = customer,
TrackingState = TrackingState.Added
};
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.NotEmpty(changes);
Assert.Equal(TrackingState.Unchanged, changes.First().TrackingState);
Assert.Equal(TrackingState.Added, changes.First().CustomerSetting.TrackingState);
}
示例9: GetChanges_On_Existing_Customer_With_Removed_CustomerSetting_Return_Empty_Collection
public void GetChanges_On_Existing_Customer_With_Removed_CustomerSetting_Return_Empty_Collection()
{
// Arrange
var database = new MockNorthwind();
var customer = database.Customers[0];
customer.CustomerSetting = new CustomerSetting
{
CustomerId = customer.CustomerId,
Setting = "Setting1",
Customer = customer
};
var customerSetting = customer.CustomerSetting;
var changeTracker = new ChangeTrackingCollection<Customer>(customer);
customer.CustomerSetting = null;
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.Empty(changes);
Assert.Equal(TrackingState.Unchanged, customerSetting.TrackingState);
}
示例10: GetChanges_On_Existing_Order_With_Removed_Customer_Should_Return_Empty_Collection
public void GetChanges_On_Existing_Order_With_Removed_Customer_Should_Return_Empty_Collection()
{
// NOTE: Reference properties are change-tracked but do not call
// NotifyPropertyChanged because it is called by foreign key's property setter.
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(order);
var customer = order.Customer;
order.Customer = null;
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.Empty(changes);
Assert.Equal(TrackingState.Unchanged, customer.TrackingState);
}
示例11: GetChanges_On_Existing_Order_With_Manually_Added_Customer_Should_Return_Customer_Marked_As_Added
public void GetChanges_On_Existing_Order_With_Manually_Added_Customer_Should_Return_Customer_Marked_As_Added()
{
// NOTE: Reference properties must be explicitly marked as Added.
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(order);
order.Customer = new Customer()
{
CustomerId = "ABCD",
CustomerName = "Test",
TrackingState = TrackingState.Added
};
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.NotEmpty(changes);
Assert.Equal(TrackingState.Unchanged, changes.First().TrackingState);
Assert.Equal(TrackingState.Added, changes.First().Customer.TrackingState);
}
示例12: GetChanges_On_Existing_Order_With_New_Customer_Should_Return_Empty_Collection
public void GetChanges_On_Existing_Order_With_New_Customer_Should_Return_Empty_Collection()
{
// NOTE: Reference properties not explicitly marked as Added will be considered
// unchanged.
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(order);
order.Customer = new Customer()
{
CustomerId = "ABCD",
CustomerName = "Test"
};
// Act
var changes = changeTracker.GetChanges();
// Assert
Assert.Empty(changes);
}
示例13: GetChanges_On_Existing_Employee_With_Territory_And_Modified_Area_Should_Return_Marked_Territory
public void GetChanges_On_Existing_Employee_With_Territory_And_Modified_Area_Should_Return_Marked_Territory()
{
// Ensure that changes are retrieved across M-M relationships.
// Arrange
var database = new MockNorthwind();
var employee = database.Employees[0];
var territory = employee.Territories[0];
var area = new Area
{
AreaId = 1,
AreaName = "Northern",
TrackingState = TrackingState.Modified
};
territory.AreaId = 1;
territory.Area = area;
var changeTracker = new ChangeTrackingCollection<Employee>(employee);
area.AreaName = "xxx"; // Modify area
// Act
var changes = changeTracker.GetChanges();
var changedEmployee = changes.First();
var changedTerritory = changedEmployee.Territories.Single(t => t.TerritoryId == territory.TerritoryId);
var changedArea = changedTerritory.Area;
// Assert
Assert.Equal(TrackingState.Unchanged, changedEmployee.TrackingState);
Assert.Equal(TrackingState.Unchanged, changedTerritory.TrackingState);
Assert.Equal(TrackingState.Modified, changedArea.TrackingState);
Assert.Contains("AreaName", area.ModifiedProperties);
}
示例14: GetChanges_On_Unchanged_Order_With_Details_Should_Return_Marked_Children
public void GetChanges_On_Unchanged_Order_With_Details_Should_Return_Marked_Children()
{
// Arrange
var database = new MockNorthwind();
var order = database.Orders[0];
var order3 = database.Orders[3];
var changeTracker = new ChangeTrackingCollection<Order>(order, order3);
order3.OrderDate += new System.TimeSpan(1, 0, 0, 0); // + one day
order3.Customer.CustomerName += " (test)";
var orderDetails = (IList<OrderDetail>)changeTracker[0].OrderDetails;
var unchangedDetail = orderDetails[0];
var modifiedDetail = orderDetails[1];
var deletedDetail = orderDetails[2];
var addedDetail = new OrderDetail
{
ProductId = 1,
Product = database.Products[0],
Quantity = 10,
UnitPrice = 20M
};
orderDetails.Add(addedDetail);
modifiedDetail.UnitPrice++;
orderDetails.Remove(deletedDetail);
// Act
var changes = changeTracker.GetChanges();
// Assert
var changedOrder = changes.First();
var changedOrder3 = changes[1];
var changedModifiedDetail = changedOrder.OrderDetails.Single(d => d.ProductId == modifiedDetail.ProductId);
var changedAddedDetail = changedOrder.OrderDetails.Single(d => d.ProductId == addedDetail.ProductId);
var changedDeletedDetail = changedOrder.OrderDetails.Single(d => d.ProductId == deletedDetail.ProductId);
Assert.Equal(TrackingState.Unchanged, changedOrder.TrackingState);
Assert.Equal(3, changedOrder.OrderDetails.Count);
Assert.Equal(TrackingState.Modified, changedModifiedDetail.TrackingState);
Assert.Equal(TrackingState.Added, changedAddedDetail.TrackingState);
Assert.Equal(TrackingState.Deleted, changedDeletedDetail.TrackingState);
Assert.DoesNotContain(unchangedDetail, changedOrder.OrderDetails);
Assert.NotNull(order.Customer);
Assert.NotNull(order3.Customer);
Assert.NotNull(changedOrder.Customer);
Assert.NotNull(changedOrder3.Customer);
Assert.True(object.ReferenceEquals(order.Customer, order3.Customer));
Assert.False(object.ReferenceEquals(order.Customer, changedOrder.Customer));
Assert.True(object.ReferenceEquals(changedOrder.Customer, changedOrder3.Customer));
}
示例15: GetChanges_On_Modified_Employee_With_Unchanged_Territories_Should_Return_No_Territories
public void GetChanges_On_Modified_Employee_With_Unchanged_Territories_Should_Return_No_Territories()
{
// Arrange
var database = new MockNorthwind();
var employee = database.Employees[0];
var changeTracker = new ChangeTrackingCollection<Employee>(employee);
employee.LastName = "xxx";
// Act
var changes = changeTracker.GetChanges();
var changedEmployee = changes.First();
// Assert
Assert.Empty(changedEmployee.Territories);
}