首页 > 解决方案 > 将 Where() 用于 Azure 移动服务的问题

问题描述

我有以下不起作用的测试:

public class DesktopDTO
{
    public DesktopDTO() {}
    public DesktopDTO(string title, Guid otherId) 
    {
         Id = Guid.NewGuid();
         Title = title;
         OtherId = otherId;
    }
    public Guid Id { get; set; }
    public string Title { get; set; }
    public Guid OtherId { get; set; }
}

//setup environment:
MobileServiceClient mobileService = new MobileServiceClient("http://myserver.azurewebsites.net/");
IMobileServiceSyncTable<DesktopDTO> table = mobileService.GetSyncTable<DesktopDTO>();
if (!mobileService.SyncContext.IsInitialized)
{
    var store = new MobileServiceSQLiteStore("localstore1.db");
    store.DefineTable<DesktopDTO>();
    await mobileService.SyncContext.InitializeAsync(store);
}
DesktopDTO input = new DesktopDTO("test124", Guid.NewGuid()); //this is my entity

//invoke action:
await table.InsertAsync(input);

//check results:
List<DesktopDTO> all = await table.ToListAsync();  //this returns 1 item
DesktopDTO r1 = all.Where(x => x.Id == input.Id).FirstOrDefault();  //this returns the created item
var query12 = await table.Where(x => x.Title == "test124").ToCollectionAsync(); //this returns 1 item
DesktopDTO r = (await table.Where(x => x.Id == input.Id).ToCollectionAsync()).FirstOrDefault(); //this returns null!!

问题是最后一个本地查询,它使用Where()由 Id 过滤的子句(这是DesktopDTO实体的 PK),不返回想要的实体。

该实体已在数据库中正确插入(如其他查询所示,即使是由“标题”过滤的实体),所以我不明白为什么Where()过滤器不应仅与 PK 一起使用。

我也尝试使用该LookupAsync()方法,但再次没有结果。

我究竟做错了什么?

谢谢!

标签: c#sqliteazureazure-mobile-services

解决方案


我尝试在我这边重现这个问题。但我得到了 ArgumentException:“id 必须是字符串类型”。

在此处输入图像描述

如果我将 Id 类型从Guid更改 为string,我将无法重现您提到的问题。我在我身边工作正常。

public class DesktopDTO
    {
        public DesktopDTO() { }
        public DesktopDTO(string title, Guid otherId)
        {
            Id = Guid.NewGuid().ToString();
            Title = title;
            OtherId = otherId;
        }
        public string Id { get; set; }
        public string Title { get; set; }
        public Guid OtherId { get; set; }

    }

测试结果:

在此处输入图像描述


推荐阅读