Azure 移动服务软删除问题/实践

标签 azure synchronization azure-mobile-services

打开软删除后,我在客户端上添加一条记录,推送,删除添加的记录推送,然后尝试使用与初始记录相同的主键添加新记录(然后推送),我得到一个异常(exception)。 EntityDomainManager 似乎只是尝试执行新插入,而不检查记录是否要“更新”而不是插入。

但是,如果我在域管理器构造函数中关闭软删除,则一切正常。

我们正在使用增量同步,因此据我所知,需要软删除才能完成这项工作,因此我们最终不会得到移动设备和服务器之间正确情况的不同图片。

什么时候推荐采用这种方法?自定义 EntityDomainManager(或其他 DomainManager)?如果是这样,这对于更清楚地了解表 Controller 和域管理器之间的交互将很有用。

我已经构建了这个自定义域管理器,它似乎可以工作,但希望得到任何指导/建议。

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);
    }

最佳答案

此行为是设计使然 - 我们要求您在执行更新之前执行显式取消删除。

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

关于Azure 移动服务软删除问题/实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26905207/

相关文章:

azure - windows azure中一个应用程序下的多个webroles/workerroles的目的是什么

linux - 什么是内存互锁?

c# - 如何使用 Monitor/Mutex/Semaphore 同步 TPL 任务?还是应该完全使用其他东西?

Java 多线程共享同一个对象的同步问题

Azure错误: Client side authentication flow with Google is not supported

android - 从 Azure 获取结果,插入一行并在插入 Azure 数据库后从本地数据库删除行数据

c# - 使用无效键值设置 JArray 值 : "version". 需要 Int32 数组索引

Azure 网站和 VM 位于同一数据中心

azure - Spring Security支持多种身份验证类型

linux - 将 Azure 上的 LAMP 服务器连接到 websocket 服务器