2013-04-13 45 views
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 

我還可以添加之間的關係地址和客戶

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

但是,我不能添加第二個相關的地址上下文:

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

this post我瞭解,這是enougth只是插入相關實體是這樣的:

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 :(

這將是完美的,但它dosn沒有工作。該錯誤顯示一個外鍵錯誤。我有沒有設置錯誤?我的EF看起來是這樣的:

EF Model

我必須對foregin關鍵屬性添加的內容:(我使用的模型首先計算策略) enter image description here

作爲一個額外的節點:我使用SQL Azure的數據庫,所以組合鍵不準:(

回答

0

可能是我錯了,但不同的是,在後您引用的MemberComments(你的情況 - CustomerAddress) - 是集:

public virtual ICollection<MemberComment> MemberComments { get; set; } 

並添加項目時,將他們不要的上下文,而是要收藏:

上下文。 會員評論 .Add(memberComment1); //還將添加member1 和comment1上下文。 會員評論 .Add(memberComment2); //也 將增加註釋2像

方面

所以,你的情況,很可能,它應該是什麼。 your_defined_CustomerAddressesList .add(customerAddress); 上下文。 your_defined_CustomerAddressesList .add(customerAddress2);