I am wondering how Entity Framework works with pure functions. I have an order class with an update status method. Instead of changing the property of the current instance, it returns a new instance with the property update. This works as expected.
public class Order {
...
private Order(Order order, string status){
Id = order.Id;
...
Status = status;
}
Guid Id { get; private set; }
...
string Status { get; private set; }
public Order UpdateStatus(string status){
if (Status == "Complete"){
return this;
}
return new Order(this, status);
}
}
The problem comes when using Entity Framework. Since the order returned, is not the same instance being tracked by the change tracker, entity framework doesn't correctly save the changes.
var order = this.dbContext.Orders.Single(o => o.Id == id);
var newOrder = order.UpdateStatus();
this.dbContext.SaveChanges();
Is there a better way to do this to have a pure function and still have the changes tracked?