1
Vote

Store is inserting duplicates instead of updating

description

My issue is that NDatabase does not detect my object reference after closing and reopening the IOdb, which causes it to try to insert a duplicate instead of performing and update.

Scenario #1 - Not closing and reopening the IOdb:
using (var db = CreateDB())
{
    var order = db.AsQueryable<Order>().Where(p => p.OrderID == 100).FirstOrDefault();
    var order.TimeStamp = DateTime.Now;
    db.Store(order); // This updates as expected!
}
Scenario #2 - Closing and reopening the IOdb:
public class OrderRepository : RepositoryBase
{
    public Order GetOrder(int orderID)
    {
       using (var db = base.CreateDB())
       {
          return db.AsQueryable<Order>().Where(p => p.OrderID == 100).FirstOrDefault();
       }
    }

    public void Save(Order order)
    {
       using (var db = base.CreateDB())
       {
            db.Store(order);        
       }
    }
}
The calling code:
var repository = new OrderRepository();
var order = repository.GetOrder(100);
var order.TimeStamp = DateTime.Now;
repository.Save(order); // order object is not recognized!
In this scenario, the order object is not recognized, even though it is the exact same object reference! This causes unwanted duplicate orders to be stored.

Possible Workarounds:
1: It would seem that the only work around currently would be for my repository Save method to requery the order, then do a deep clone to the requeried order, and then save the requeried order.
2: I could query and delete the old object and then call Store.

It would be ideal if the object reference detection worked even if the IOdb object was closed and reopened.


Thanks,
Jordan

comments