2017-01-04 9 views
0

Xamarin.FormsモバイルアプリからParent and ChildオブジェクトをAzure Mobile Appsサーバーに挿入したいと思います。私は、TableController Postメソッドを介してParentおよび/またはChildオブジェクトを取得できますが、ChildをParentに関連付けることはできません。Azure Mobile Apps TableControllerとの関係を挿入

Parentが挿入されますが、Parentのユニークなキー制約が違反しているため、Childの挿入が失敗します(リンクではなく別のParentを挿入しようとしていると思います)。

私は親を挿入せずに子を挿入しようとした場合、それは親を挿入し

が、000 ... 000 Parent.Idと

これは非常に一般的な作業のように思えるが、私はありません見つけることができます有用なチュートリアルまたは答え。私が見つけることができるすべてのまともなAzure Mobile Appsチュートリアルは、動作する単一テーブルのToDoの例から動作していますが、動作しますが、リレーショナルの例はありません。

私のサーバー側のモデルは以下のとおりです。

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ポスト(親のために)のようになります。

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

と(子供用):

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

答えて

1

あなたはAzureのモバイルアプリとの直接の関係を行うことはできません。

この関係を切り離してから、各テーブルを個別にプッシュする必要があります。

http://aka.ms/zumobookを読んでください。特に関係のテーマを扱う第3章を読んでください。

+0

ありがとうございます。その本は素晴らしいリソースのように見えます!私はなぜそれがおそらく8時間の検索と試しに出てこなかったのか分かりません。 Googleは良いものを私に指示する必要があります! –

+0

この本は私がおそらく最後の2ヶ月間答えたすべてのSOの質問の答えの一部となっています。 –

+0

ここでエラー処理のベストプラクティスは何ですか? など。 1つのテーブルをプッシュし、2番目のテーブルのプッシュは失敗します。 – Robert

関連する問題