Azure Mobile Service TableController不返回内部对象

我正在使用表存储创建一个基本(我的第一个)Azure移动服务来控制一个简单的事件应用程序。 我的DataObjects由2个对象类型组成: CoordinatorEvent ,我希望Coordinators是一个单独的表来存储特定信息,我不希望它在事件中被非规范化,但是事件还有一个内部对象Location来存储事件的详细信息位置,但我想存储非规范化,因为我不想将此细节与事件分开维护。

这是我到目前为止的对象:DataObjests:

 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; } } 

作为VS脚手架生成的基本TableController的控制器,我做的唯一改变是在事件控制器上公开MobileServiceContext以使Post方法在保存时找到现有的Coordinator,因为客户端只会发布Coordinator的ID:

 public class EventController : TableController { private MobileServiceContext context; protected override void Initialize(HttpControllerContext controllerContext) { base.Initialize(controllerContext); context = new MobileServiceContext(); DomainManager = new EntityDomainManager(context, Request, Services); } protected override void Dispose(bool disposing) { context?.Dispose(); base.Dispose(disposing); } public IQueryable GetAllEvent() { return Query(); } public async Task 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); } } 

从客户端发布数据按预期工作,我有一个包含正确数据的Coordinator表:

 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 

与第一个协调员关联的事件:

 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个问题是Get of the Event,其中没有返回CoordinatorLocation对象,而我的EventController的Get的Json就是这样:

 [{"id":"961abbf839bf4e3481ff43a214372b7f","attendees":0,"eventDate":"2015-11-04T09:00:00Z"}] 

所以我的两个问题是:

1)Location对象由服务器上的EventController正确加载,如果我在返回之前中断,我可以看到正确加载的属性,对我来说看起来像Json序列化问题,但我已经尝试更改序列化程序的配置(在WebApiConfig )没有太大影响,我尝试的最后一个选项是MaxDepth但仍然没有返回Location对象。

2)协调器对象我根本没有加载到服务器上,甚至没有Id(正确存储在表上)所以我不能强制加载整个对象,当然它不会返回给客户端。

我在这里做错了什么想法?

提前致谢

Claiton Lovato

这是TableController的默认行为。 为了实现您正在寻找的东西,您应该在控制器中实现OData $ expand

本文为解决方案提供了良好的步骤
使用.NET后端Azure移动服务从1:n关系中检索数据

作为进一步的扩展,我实现了一个自定义属性,您可以在控制器方法中使用该属性来指定客户端可以请求扩展的属性。 您可能不想总是返回所有子关系(扩展对象)

 [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] public class ExpandablePropertyAttribute : ActionFilterAttribute { #region [ Constants ] private const string ODATA_EXPAND = "$expand="; #endregion #region [ Fields ] private string _propertyName; private bool _alwaysExpand; #endregion #region [ Ctor ] public ExpandablePropertyAttribute(string propertyName, bool alwaysExpand = false) { this._propertyName = propertyName; this._alwaysExpand = alwaysExpand; } #endregion #region [ Public Methods - OnActionExecuting ] public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); var uriBuilder = new UriBuilder(actionContext.Request.RequestUri); var queryParams = uriBuilder.Query.TrimStart('?').Split(new char[1] { '&' }, StringSplitOptions.RemoveEmptyEntries).ToList(); int expandIndex = -1; for (var i = 0; i < queryParams.Count; i++) { if (queryParams[i].StartsWith(ODATA_EXPAND, StringComparison.Ordinal)) { expandIndex = i; break; } } if (expandIndex >= 0 || this._alwaysExpand) { if (expandIndex < 0) { queryParams.Add(string.Concat(ODATA_EXPAND, this._propertyName)); } else { queryParams[expandIndex] = queryParams[expandIndex] + "," + this._propertyName; } uriBuilder.Query = string.Join("&", queryParams); actionContext.Request.RequestUri = uriBuilder.Uri; } } #endregion } 

我以这种方式在我的控制器中使用它:

  [ExpandableProperty("Documents", false)] public IQueryable GetAllClientActivities() { return Query(); } 

我已设法将复杂的属性序列化并发送回客户端。 也许解决方案不是最干净的,但它适用于我的情况。 希望其他人也会觉得它很有用。

首先,创建一个inheritance自GetModel的类,并覆盖GetModel方法,如下所示:

 public class CustomEnableQueryAttribute : EnableQueryAttribute { public override IEdmModel GetModel(Type elementClrType, HttpRequestMessage request, HttpActionDescriptor actionDescriptor) { var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet("Events"); return modelBuilder.GetEdmModel(); } } 

然后,在控制器的Initialize方法中,添加以下行:

  protected override void Initialize(HttpControllerContext controllerContext) { base.Initialize(controllerContext); context = new MobileServiceContext(); DomainManager = new EntityDomainManager(context, Request, Services); //Add these lines var service = Configuration.Services.GetServices(typeof(IFilterProvider)).ToList(); service.Remove(service.FirstOrDefault(f => f.GetType() == typeof(TableFilterProvider))); service.Add(new TableFilterProvider(new CustomEnableQueryAttribute())); Configuration.Services.ReplaceRange(typeof(IFilterProvider), service.ToList().AsEnumerable()); Request.Properties.Add("MS_IsQueryableAction", true); }