将关系插入到 Azure 移动应用程序 TableController
Insert with Relation into Azure Mobile Apps TableController
我想将 Parent 和 Child object 从 Xamarin.Forms 移动应用插入到 Azure 移动应用服务器中。我可以通过 TableController Post 方法获得 Parent and/or 或 Child object,但我永远无法获得 Child与 Parent.
有关
下面代码片段的结果是 Parent 被插入,但是 Child 插入失败,因为在 Parent 上违反了唯一键约束(我相信正在尝试插入另一个 Parent 而不是链接)。
如果我只是尝试插入 Child 而没有插入 Parent 那么它会插入一个 Parent 但带有 000...000 Parent.Id
这似乎是一项很常见的任务,但我找不到有用的教程或答案。我能找到的每个像样的 Azure 移动应用程序教程都来自 single-table ToDo 示例,该示例有效但缺少关系示例。
我的 server-side 模特是:
public class Parent : EntityData
{
public string Description { get; set; }
//public ICollection<Child> Childs { get; set; }
}
public class Child : EntityData
{
public string Description { get; set; }
public Parent Parent { get; set; }
}
我的服务器 TableController Post 看起来像(对于 Parent):
public class ParentController : TableController<Parent>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext();
DomainManager = new EntityDomainManager<Parent>(context, Request);
}
// POST tables/Parent
public async Task<IHttpActionResult> PostParent(Parent item)
{
Parent current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
}
和(对于 Child):
public class ChildController : TableController<Child>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext();
DomainManager = new EntityDomainManager<Child>(context, Request);
}
// POST tables/Child
public async Task<IHttpActionResult> PostChild(Child item)
{
Child current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
}
在Xamarin.Forms移动端我有模型:
public class Parent
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
}
public class Child
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
public Parent Parent { get; set; }
}
并且 Xamarin.Forms 插入命令是:
private async Task PostParentTask()
{
Parent Parent = new Parent { Description = "New Parent" };
await App.MobileService.GetTable<Parent>().InsertAsync(Parent);
await Application.Current.MainPage.DisplayAlert("Parent", string.Format("{0} was posted with GUID {1}", Parent.Description, Parent.Id), "OK");
Child Child = new Child { Description = "New Child", Parent = Parent};
await App.MobileService.GetTable<Child>().InsertAsync(Child);
}
您不能与 Azure 移动应用建立直接关系。
你必须解耦关系,然后分别推送每个table。
阅读http://aka.ms/zumobook - 特别是涉及关系主题的第 3 章。
我想将 Parent 和 Child object 从 Xamarin.Forms 移动应用插入到 Azure 移动应用服务器中。我可以通过 TableController Post 方法获得 Parent and/or 或 Child object,但我永远无法获得 Child与 Parent.
有关下面代码片段的结果是 Parent 被插入,但是 Child 插入失败,因为在 Parent 上违反了唯一键约束(我相信正在尝试插入另一个 Parent 而不是链接)。
如果我只是尝试插入 Child 而没有插入 Parent 那么它会插入一个 Parent 但带有 000...000 Parent.Id
这似乎是一项很常见的任务,但我找不到有用的教程或答案。我能找到的每个像样的 Azure 移动应用程序教程都来自 single-table ToDo 示例,该示例有效但缺少关系示例。
我的 server-side 模特是:
public class Parent : EntityData
{
public string Description { get; set; }
//public ICollection<Child> Childs { get; set; }
}
public class Child : EntityData
{
public string Description { get; set; }
public Parent Parent { get; set; }
}
我的服务器 TableController Post 看起来像(对于 Parent):
public class ParentController : TableController<Parent>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext();
DomainManager = new EntityDomainManager<Parent>(context, Request);
}
// POST tables/Parent
public async Task<IHttpActionResult> PostParent(Parent item)
{
Parent current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
}
和(对于 Child):
public class ChildController : TableController<Child>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
MyContext context = new MyContext();
DomainManager = new EntityDomainManager<Child>(context, Request);
}
// POST tables/Child
public async Task<IHttpActionResult> PostChild(Child item)
{
Child current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
}
在Xamarin.Forms移动端我有模型:
public class Parent
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
}
public class Child
{
[JsonProperty(PropertyName = "Id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "Description")]
public string Description { get; set; }
public Parent Parent { get; set; }
}
并且 Xamarin.Forms 插入命令是:
private async Task PostParentTask()
{
Parent Parent = new Parent { Description = "New Parent" };
await App.MobileService.GetTable<Parent>().InsertAsync(Parent);
await Application.Current.MainPage.DisplayAlert("Parent", string.Format("{0} was posted with GUID {1}", Parent.Description, Parent.Id), "OK");
Child Child = new Child { Description = "New Child", Parent = Parent};
await App.MobileService.GetTable<Child>().InsertAsync(Child);
}
您不能与 Azure 移动应用建立直接关系。
你必须解耦关系,然后分别推送每个table。
阅读http://aka.ms/zumobook - 特别是涉及关系主题的第 3 章。