【发布时间】:2015-09-04 09:54:44
【问题描述】:
我正在使用表存储创建一个基本的(我的第一个)Azure 移动服务来控制一个简单的事件应用程序。我的 DataObjects 由 2 种对象类型组成:Coordinator 和 Event,我希望协调器成为一个单独的表来存储我不希望它在事件中非规范化的特定信息,但事件也有一个内部对象位置来存储事件位置的详细信息,但我想非规范化存储,因为我不想将这些详细信息与事件分开维护。
这是我目前拥有的对象: 数据对象:
public class Coordinator : EntityData {
public string Name { get; set; }
public int Points { get; set; }
public bool IsActive { get; set; }
}
public class Event: EntityData {
public Coordinator Coordinator { get; set; }
public DateTime EventDate { get; set; }
public int Attendees { get; set; }
public Location Location { get; set; }
}
public class Location {
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string County { get; set; }
public string PostCode { get; set; }
}
控制器作为基本的TableController由VS的脚手架生成,我所做的唯一更改是在事件控制器上公开MobileServiceContext以启用Post方法在保存时找到现有的协调器,因为客户端只会发布协调员 ID:
public class EventController : TableController<Event> {
private MobileServiceContext context;
protected override void Initialize(HttpControllerContext controllerContext) {
base.Initialize(controllerContext);
context = new MobileServiceContext();
DomainManager = new EntityDomainManager<Event>(context, Request, Services);
}
protected override void Dispose(bool disposing) {
context?.Dispose();
base.Dispose(disposing);
}
public IQueryable<Event> GetAllEvent() {
return Query();
}
public async Task<IHttpActionResult> PostEvent(Event item) {
var coordinator = context.Coordinators.Find(item.Coordinator.Id);
item.Coordinator = coordinator;
Event current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
}
从客户端发布数据按预期工作,我有一个包含正确数据的协调器表:
ID Name Points IsActive Version CreatedAt UpdatedAt Deleted
cdf96b0f93644f1e945bd16d63aa96e0 John Smith 10 True 0x00000000000007D2 04/09/2015 09:15:02 +00:00 04/09/2015 09:15:02 +00:00 False
f216406eb9ad4cdcb7b8866fdf126727 Rebecca Jones 10 True 0x00000000000007D4 04/09/2015 09:15:30 +00:00 04/09/2015 09:15:30 +00:00 False
还有一个与第一个 Coordinator 关联的事件:
Id EventDate Attendees Location_Name Location_Address1 Location_Address2 Location_City Location_County Location_PostCode Version CreatedAt UpdatedAt Deleted Coordinator_Id
961abbf839bf4e3481ff43a214372b7f 04/11/2015 09:00:00 0 O2 Arena Peninsula Square London SE10 0DX 0x00000000000007D6 04/09/2015 09:18:11 +00:00 04/09/2015 09:18:11 +00:00 False cdf96b0f93644f1e945bd16d63aa96e0
此时一切看起来都不错,我的 2 个问题与事件的获取有关,其中 Coordinator 和 Location 对象均未返回,而我的 EventController 获取的 Json 就是这样:
[{"id":"961abbf839bf4e3481ff43a214372b7f","attendees":0,"eventDate":"2015-11-04T09:00:00Z"}]
所以我的两个问题是:
1) Location 对象由服务器上的EventController 正确加载,如果我在返回之前中断,我可以看到属性正确加载,对我来说这看起来像是 Json 序列化问题,但我已经尝试更改序列化程序的配置(在WebApiConfig上)没有太大影响,我尝试的最后一个选项是MaxDepth,但仍然没有返回Location对象。
2) Coordinator 对象我根本没有加载到服务器上,甚至没有 Id(正确存储在表中)所以我不能强制加载整个对象,当然它不会返回到客户。
关于我在这里做错了什么有什么想法吗?
提前致谢
克莱顿·洛瓦托
【问题讨论】:
标签: c# azure azure-mobile-services azure-table-storage