0

oData を実行している WCF データ サービスがあり、それにデータを挿入しようとしています。Windows 8 クライアントから非同期を試みています。

私のエンティティは次のようになります。

Customer 1 -> m CustomerAddress 1 <- m Address

AddObject メソッドを呼び出して変更を保存するだけで、住所または顧客を挿入できます。

context.AddObject("Customer",new Customer { FirstName="foo" });
context.AddObject("Address",new Address { Street="bar" });
await context.SaveChangesBatch(); //a extension class that handels the BeginSaveChanges async calls as batch

Address と Customer の間にリレーションを追加することもできます

Customer customer=new Customer { FirstName="foo" };
Address orderAddress=new Address { Street="bar" };
CustomerAddress customerAddress = new CustomerAddress { Customer = customer, Address = orderAddress };
context.AddObject("Customer",customer);
context.AddObject("Address",orderAddress);
context.AddObject("CustomerAddress",customerAddress);
await context.SaveChangesBatch(); 

しかし、関連する 2 つ目の Address をコンテキストに追加することはできません。

Address orderAddress=new Address { Street="bar" };
Address deliveryAddress=new Address { Street="bar2" };
CustomerAddress customerAddress = new CustomerAddress { Customer = customer, Address = orderAddress };
Customer customer=new Customer { FirstName="foo" };
CustomerAddress customerAddress = new CustomerAddress { Customer = customer, Address = deliveryAddress };
context.AddObject("Customer",customer);
context.AddObject("Address",orderAddress);
context.AddObject("Address",deliveryAddress);
context.AddObject("CustomerAddress",customerAddress);
context.AddObject("CustomerAddress",customerAddress2);
await context.SaveChangesBatch(); 

この投稿では、次のように関連するエンティティを挿入するだけで十分であると読みました。

CustomerAddress customerAddress = new CustomerAddress { Customer = customer, Address = orderAddress };
CustomerAddress2 customerAddress = new CustomerAddress { Customer = customer, Address = deliveryAddress };
context.AddObject("CustomerAddress",customerAddress);
context.AddObject("CustomerAddress",customerAddress2);
await context.SaveChangesBatch(); //On save, it should automatically insert customer and order but it dosn't work :(

これは完璧ですが、うまくいきません。エラーは、外部キー エラーを示しています。何か設定が間違っていますか?私のEFは次のようになります。

EFモデル

foregin キーのプロパティに何かを追加する必要がありますか: (私はモデルの最初のアプローチを使用しています) ここに画像の説明を入力

追加ノードとして: Azure SQL データベースを使用しているため、複合キーは使用できません:(

4

1 に答える 1