【发布时间】:2016-05-25 14:08:20
【问题描述】:
我正在使用 Azure 应用程序服务(移动应用程序)来开发一个使用 SQLite 的离线同步功能的应用程序。 我的数据对象模型是:
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; }
}
我的控制器代码是:
// 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);
}
我不太确定这在幕后是如何映射的,但我从我的客户那里打来电话:
await App.MobileService.SyncContext.PushAsync();
await customerTable.PullAsync("customers", customerTable.CreateQuery());
当我执行数据的初始同步以插入客户时,一切正常,但是,当我尝试更新其中任何一个时,我收到一个错误“操作因冲突而失败:'违反 PRIMARY KEY 约束'PK_dbo 。牌'。无法在对象“dbo.Cards”中插入重复键。重复键值为 (000000003414)。注意我没有更改卡的详细信息,只更改了客户。 我看过这个帖子:https://blogs.msdn.microsoft.com/azuremobile/2014/06/18/insertupdate-data-with-1n-relationship-using-net-backend-azure-mobile-services/ 然而,它似乎过于复杂,我不确定这是否是我所追求的……有人知道发生了什么吗?
【问题讨论】:
-
我怀疑您正在查看实体框架问题,因为 Id 是一个字符串 - 而不是一个 int。在 Customer 对象中将适当的 CardId 设置为字符串,然后通过 ForeignKey 注释链接,也许?
-
移动服务要求 Id 为字符串,不知道为什么...我的控制器实现只是在您提供移动服务后端时对示例解决方案提供的函数的重命名...
-
您在
PatchCustomer上设置了断点吗?还是再次点击插入? -
我在 PatchCustomer 上放了一个断点,它正确地击中了它。然后它正在调用 UpdateAsync 并返回如上所述的错误...
标签: c# sqlite azure azure-mobile-services