【发布时间】:2014-11-18 04:16:16
【问题描述】:
打开软删除后,我在客户端添加一条记录,推送,删除添加的记录推送,然后尝试添加一条与初始记录具有相同主键的新记录(然后推送)我得到一个例外。 EntityDomainManager 似乎只是尝试进行新的插入,而不检查记录是否要“更新”而不是插入。
但是,如果我在域管理器构造函数中关闭软删除,一切正常。
我们正在使用增量同步,因此据我所知需要软删除才能使这项工作正常进行,因此我们最终不会在移动设备和服务器之间得到不同的图片。
推荐的方法是什么时候?自定义 EntityDomainManager(或其他 DomainManager)?如果是这样,这将有助于更清楚地了解表控制器和域管理器之间的交互。
我已经构建了这个似乎可以工作的自定义域管理器,但希望得到任何指导/建议。
public class CustomEntityDomainManager<TData> : EntityDomainManager<TData> where TData : class, ITableData
{
public CustomEntityDomainManager(DbContext context, HttpRequestMessage request, ApiServices services)
: base(context, request, services)
{
}
public CustomEntityDomainManager(DbContext context, HttpRequestMessage request, ApiServices services, bool enableSoftDelete) : base(context, request, services, enableSoftDelete)
{
}
public async override Task<TData> InsertAsync(TData data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
// now then, if we have soft delete enabled & data has been provided with an id in it
if (EnableSoftDelete && data.Id != null)
{
// now look to see if the record exists and if it is deleted
// if so we look to remove the record before then attempting the insert
// record old value of deleted, since need to query to see if deleted.
var oldIncludeDeleted = IncludeDeleted;
try
{
IncludeDeleted = true;
var existingData = await this.Lookup(data.Id).Queryable.FirstOrDefaultAsync();
// if record exists, and its soft deleted then truly delete it
if (existingData != null && existingData.Deleted)
{
// now need to remove this record...
this.Context.Set<TData>().Remove(existingData);
}
}
finally
{
IncludeDeleted = oldIncludeDeleted;
}
}
if (data.Id == null)
{
data.Id = Guid.NewGuid().ToString("N");
}
return await base.InsertAsync(data);
}
【问题讨论】:
标签: azure synchronization azure-mobile-services