【问题标题】:Azure Mobile Services Soft Delete Issue / PracticesAzure 移动服务软删除问题/实践
【发布时间】: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


    【解决方案1】:

    此行为是设计使然——我们要求您在进行更新之前明确取消删除。

    您提出的解决方案很好。您还可以将代码移动到您的表格控制器,假设您只需要在一个表格中使用此行为。如果您需要在多个表中使用它,那么自定义域管理器是最好的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多