我有一個運行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看起來是這樣的:
我必須對foregin關鍵屬性添加的內容:(我使用的模型首先計算策略)
作爲一個額外的節點:我使用SQL Azure的數據庫,所以組合鍵不準:(