c# - 将 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()
方法,但再次没有结果。
我究竟做错了什么?
谢谢!
解决方案
我尝试在我这边重现这个问题。但我得到了 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; }
}
测试结果:
推荐阅读
- entity-framework - 无法隐式转换类型'System.Collections.Generic.List
' 到 'DistributionEmail.Data.EmailInfo' - python - 试图抓取 forecast.weather.gov 并遇到问题,特别是从 WriteText 标签获取数据
- python - 使用系列的近似 pi
- java - Spring RestTemplate JSON 响应被截断
- ios - 如何对 Realm 中的对象进行异步排序
- node.js - 无法扩展接口“http.IncomingMessage”。您指的是 “implements” 吗?
- python - 我在运行 train.py 时遇到问题,我很困惑
- javascript - HTML5 drawImage:Chrome 比 Mozilla 慢九倍
- azure - Azure 资源
- python - 无法使用漂亮的汤访问某些标签