Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MergeChanges Does Not Merge Child Entities Added By Database #156

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ obj
/Build/Output
/Build/Logs
/Source/.vs/config/applicationhost.config
/Samples/VS2013/WebApiSample/.vs/config/applicationhost.config
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,34 @@ public void MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_Changed_O
Assert.Empty(cachedDeletes); // Cached deletes have been removed
}

[Fact]
public void MergeChanges_Should_Merge_Updates_For_Unchanged_Order_With_DB_Added_OrderDetails()
{
// Arrange
var database = new MockNorthwind();
var origOrder = database.Orders[0];
var changeTracker = new ChangeTrackingCollection<Order>(origOrder);
origOrder.OrderDetails[0].Quantity++;
var product = database.Products.Single(p => p.ProductId == 15);
var dbAddedDetail = new OrderDetail
{
ProductId = 15,
OrderId = 10249,
Quantity = 10,
UnitPrice = 19.6000M,
Product = product
};

var changes = changeTracker.GetChanges();
var updatedOrder = UpdateOrdersWithDetails(database, changes, dbAddedDetail)[0];

// Act
changeTracker.MergeChanges(updatedOrder);

// Assert
Assert.Same(dbAddedDetail, origOrder.OrderDetails.Single(d => d.ProductId == 15)); // Ref equality
}

#endregion

#region MergeChanges: Many-to-One
Expand Down Expand Up @@ -1220,6 +1248,24 @@ private List<Order> UpdateOrdersWithDetails(MockNorthwind database, IEnumerable<
return updatedOrders;
}

private List<Order> UpdateOrdersWithDetails(MockNorthwind database, IEnumerable<Order> changes, OrderDetail dbAddedDetail = null)
{
var updatedOrders = new List<Order>();
foreach (var origOrder in changes)
{
// Simulate serialization
var updatedOrder = origOrder.Clone<Order>();

// Simulate db-generated values
updatedOrder.OrderDetails[0].UnitPrice++;
if (dbAddedDetail != null)
updatedOrder.OrderDetails.Add(dbAddedDetail);
updatedOrder.AcceptChanges();
updatedOrders.Add(updatedOrder);
}
return updatedOrders;
}

private List<Employee> UpdateEmployeesWithTerritories(IEnumerable<Employee> changes)
{
var updatedEmployees = new List<Employee>();
Expand Down