【发布时间】:2018-11-13 02:37:18
【问题描述】:
我有以下不起作用的测试:
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!!
问题是最后一个本地查询,它使用由 Id 过滤的 Where() 子句(这是 DesktopDTO 实体的 PK),没有返回想要的实体。
实体已在数据库中正确插入(如其他查询所示,即使是按“标题”过滤的实体),所以我不明白为什么 Where() 过滤器不应仅与 PK 一起使用。
我也尝试过使用LookupAsync() 方法,但还是没有结果。
我做错了什么?
谢谢!
【问题讨论】:
-
您是否尝试过将 id 作为变量而不是直接在表达式中传递?
-
是的,结果一样 :(
标签: c# sqlite azure azure-mobile-services