RIA Services: Entities Parent Child relationship
In a project using RIA Services, I've a Customer table and an Address table to store the address of each customer.
On a page in silverlight, I have the Customer data and when I expand a control, the address data for that customer are loaded.
When I create a new Customer with his address, how do I save both separetely? The Address entity contains the CustomerId. It is a required field.
How can I save the address when the customerId doesn't ex开发者_如何学运维ist? And I can also not save the customer first, because it needs the address.
What can I do?
Let me clarify something. Just because the Entities have Navigational Properties doesn't mean you can't create them. Your perception that you have a catch 22 with "can't create one without the other, but can't create the other without the one" is not quite how EF works.
There is a simple way of doing this. The following example uses an anonymous delegate (lambda exression) to create the Customer first. Once the operation has returned from the server, the delegate/lambda expression handles the completed event and allows you to save the Address for that Customer.
This model assumes you have 1 to many relationship between a Customer and Address... aka: 1 Customer can have many addresses. The Id field of the Customer is a foreign key in the Address (CustomerId).
private void SaveCustomerAndAddress()
{
CustomerContext context = new CustomerContext();
Customer c = new Customer();
c.Name = "Daniel";
context.Customers.Add(c);
context.SubmitChanges( submitChangesComplited =>
{
//Lambda expressions. Executes when the Completed event fires for the
//SubmitChanges. At this point the Customer entity that was created
//will have its Id assigned by the context, so you can access it
//from within the lambda expression block.
Address address = new Address();
address.CustomerId = c.Id;
address.Name = "Home Address";
address.State = "CA";
address.City = "San Diego";
context.Addresses.Add(address);
context.SubmitChanges();
}, null);
}
If however you have a meny-to-meny relationships between customers and addresses you have to get a bit more creative. The opeartions are as follows: 1. Save Customer 2. Save Address 3. Create Record in the joiner table for the CustomerAddress association.
Note: Using lambda expressions makes things is a bit easier because you have the ability to access variables outside the lambda expressions, so long as they are declared in the same method. Otherwise for every operation you have to persist objects into "State" if more than one operations are needed to achieve what you want:
If you are using Entity Framework, and mapped the keys and foreign keys correctly, then you don't have to specify the key manually, you just set it through a navigation property (for example c.address = MyCreatedAddress or a.customer = MyCreatedCustomer) and whichever entity you are saving first in the context, that will save the corresponding ones through the nav. properties.
One thing that you have to keep in mind, that if you have 1 to 1 relation instead of 1 to many, then the primary keys should be the same for both tables in the mapping (EF won't let you other choice afaik)
(Pretty old question but it may help someone sometime : )
精彩评论