【问题标题】:azure mobile app and MobileServicePreconditionFailedException天蓝色移动应用和 MobileServicePreconditionFailedException
【发布时间】:2016-11-01 01:39:07
【问题描述】:

我有一个带有离线同步的简单 TodoItem 示例。一切都很好。 检测到冲突并且客户端捕获 MobileServicePreconditionFailedException 异常。

现在,当我修改 TableController 以使用 Dto 映射并使用自定义 MappedEntityDomainManager 时,我从未遇到 MobileServicePreconditionFailedException 异常。

我错过了什么?

我修改了 Microsoft TodoItem 示例来演示该问题,因此唯一的区别是 DomainManager 和 TableController。

DTO 和 Entity 都是从 EntityData 派生的,因此它们确实具有所有必要的属性 Version、UpdatedAt 等......

public class TodoItemDto : EntityData
    {
        public string Text { get; set; }

        public bool Complete { get; set; }

        public bool bWorks { get; set; }
    }

public class TodoItem : EntityData
    {
        public string Text { get; set; }

        public bool Complete { get; set; }
    }

sql表和微软例子中的一样。

如果我删除 DomainManager 并在 TableController 实体中使用而不是 DTO,那么一切都很好。

谢谢

控制器如下所示:

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.Azure.Mobile.Server;
using TimeCardSyncSrv.DataObjects;
using TimeCardSyncSrv.Models;

namespace TimeCardSyncSrv.Controllers
{
    public class TodoItemController : TableController<TodoItemDto>
    {


        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            MobileServiceContext context = new MobileServiceContext();
            DomainManager = new TodoItemMappedEntityDomainManager(context, Request);
        }

        // GET tables/TodoItem
        public IQueryable<TodoItemDto> GetAllTodoItems()
        {
            return Query();
        }

        // GET tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public SingleResult<TodoItemDto> GetTodoItem(string id)
        {
            return Lookup(id);
        }

        // PATCH tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task<TodoItemDto> PatchTodoItem(string id, Delta<TodoItemDto> patch)
        {
            return UpdateAsync(id, patch);
        }

        // POST tables/TodoItem
        public async Task<IHttpActionResult> PostTodoItem(TodoItemDto item)
        {
            TodoItemDto current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

        // DELETE tables/TodoItem/48D68C86-6EA6-4C25-AA33-223FC9A27959
        public Task DeleteTodoItem(string id)
        {
            return DeleteAsync(id);
        }


        public static void AddMap(IConfiguration cfg)
        {
            cfg.CreateMap<TodoItem, TodoItemDto>();
            cfg.CreateMap<TodoItemDto, TodoItem>();

        }
    }

}

映射实体管理器如下所示:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.OData;
using Microsoft.Azure.Mobile.Server;
using TimeCardSyncSrv.DataObjects;
using TimeCardSyncSrv.Models;

namespace TimeCardSyncSrv.Controllers
{


    public class TodoItemMappedEntityDomainManager
        : MappedEntityDomainManager<TodoItemDto, TodoItem>
    {
        public TodoItemMappedEntityDomainManager(DbContext context,
            HttpRequestMessage request)
            : base(context, request)
        {
        }

        public override SingleResult<TodoItemDto> Lookup(string id)
        {
            return this.LookupEntity(p => p.Id == id);
        }

        public override Task<TodoItemDto> UpdateAsync(string id, Delta<TodoItemDto> patch)
        {
            return this.UpdateEntityAsync(patch, id);
        }

        public override Task<bool> DeleteAsync(string id)
        {
            return this.DeleteItemAsync(id);
        }
    }
}

TodoItem 和 TodoItemDto 都派生自 EntityData。

【问题讨论】:

  • 您的 DTO 是什么样的?是否有 Version、UpdatedAt 等字段? SQL 表是什么样的?
  • 阿德里安,我用信息编辑了我的问题。谢谢

标签: xamarin azure-mobile-services


【解决方案1】:

我书中的 MappedEntityDomainManager 参考:https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/domainmgr/#existing-table-relationships-with-the-mappedentitydomainmanager

您发布的信息并不表示已完成设置。我已经测试了书中的设置。您是否在启动代码中初始化了 Automapper?这似乎是可能缺少的一点。

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    相关资源
    最近更新 更多