I am using Azure App Services (Mobile Apps) to develop an application using the offline sync feature with SQLite. My data object model is:
public class Customer : EntityData
{
public string FirstName { get; set; }
public string LastName { get; set; }
//…
public DateTime DateCreated { get; set; }
public DateTime DateModified { get; set; }
public virtual IList<Card> Cards { get; set; }
}
public class Card : EntityData
{
public bool IsDisabled { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime LastUsedDate { get; set; }
}
And my controller code is:
// GET tables/Customer
public IQueryable<Customer> GetAllCustomers()
{
return Query();
}
// GET tables/Customer/48D68C86-6EA6-4C25-AA33-223FC9A27959
public SingleResult<Customer> GetCustomer(string id)
{
return Lookup(id);
}
// PATCH tables/Customers/48D68C86-6EA6-4C25-AA33-223FC9A27959
public Task<Customer> PatchCustomer(string id, Delta<Customer> patch)
{
return UpdateAsync(id, patch);
}
// POST tables/Customer
public async Task<IHttpActionResult> PostCustomer(Customer item)
{
Customer current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
// DELETE tables/Customer/48D68C86-6EA6-4C25-AA33-223FC9A27959
public Task DeleteCustomer(string id)
{
return DeleteAsync(id);
}
I am not quite sure how this is mapped behind the scene, but from my client I am calling:
await App.MobileService.SyncContext.PushAsync();
await customerTable.PullAsync("customers", customerTable.CreateQuery());
All works fine when I perform the initial sync of data to insert customers, however, when I try to update any of them I am receiving an error “The operation failed due to a conflict: 'Violation of PRIMARY KEY constraint 'PK_dbo.Cards'. Cannot insert duplicate key in object 'dbo.Cards'. The duplicate key value is (000000003414). Note I have not changed the card details, only the customer. I have seen this post: https://blogs.msdn.microsoft.com/azuremobile/2014/06/18/insertupdate-data-with-1n-relationship-using-net-backend-azure-mobile-services/ However it seems overly complex and I am not sure if it is what I am after… does anyone know what is happening?
PatchCustomer? or is it hitting insert again? - SWilko