0

I have a WCF Data Service with oData running and try to insert data into it. I'm trying async from a windows 8 client.

My entity looks like this:

Customer 1 -> m CustomerAddress 1 <- m Address

I can insert a address or a customer by simply calling the AddObject Method and Save the changes:

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

I can also add a relation between Address and 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(); 

But, I can't add a second related Address to the context:

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

In this post I read about that it is enougth to just insert the related entity like this:

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

This would be perfect, but it dosn't work. The error shows a forign key error. Do I have somethind set up wrong? My EF looks like this:

EF Model

Do I have to add something on the foregin key properties: (I'm using model first approch) enter image description here

As a additional node: I'm using Azure SQL Database, so composite keys are not allowed :(

Community
  • 1
  • 1
Jan Hommes
  • 5,122
  • 4
  • 33
  • 45

1 Answers1

0

May be I am wrong, but the difference is that in the post you referenced, the MemberComments (in your case - CustomerAddress) - is collection:

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

And when adding the items, adding them not to context, but to collection:

context.MemberComments.Add(memberComment1); // will also add member1 and comment1 context.MemberComments.Add(memberComment2); // will also add comment2

So, in your case, probably, it should be something like

context.your_defined_CustomerAddressesList.add(customerAddress); context.your_defined_CustomerAddressesList.add(customerAddress2);

evgenyl
  • 7,837
  • 2
  • 27
  • 32