This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, I have a model like this: Customer and then I have an entity called Orders. Customer has One to Many relationship with Orders. I am working on an import functionality where I recreate my existing data from an XML file. In the XML each entity is represented by a node and a reference is represented by an attribute: thus I have <customer name="Mike"/> <address value ="212 South Street"/> <customer> <Order1 name="Order1" customer="Mike"> <delivery Address value ="212 South Street"/> </Oreder1> <Order2 name = "Order2" customer = "Mike"> <delivery Address value ="212 South Street"/> </Order2> SO what happens is this: For each entity my code converts the xml into entity and then saves it. The issue I have is this: From the above file I process the Customer entity and save it with no problem. Then I go to process the first Order: Order myORder = new Order(); myOrder.Name = args["name"]; order.Customer = GetCustomerByGUID("123-455...."); the issue is that once I go to save the Order : UnitOfWork.Add(order); UnitOfWork.SaveChanges(); the unit of Work is trying to add the order an any of it's dependencies(customer being on eof them). I get an exception since I have some uniques restrictions on the customer attributes but the main question is how can I prevent the csutomer from being added twice. One thing I tried and worked is if instead of saying Order.Cusomer to say Order.CustomerId = GetCustomerByGUID("123-455....").Id but that is not good enough for me because Order has some other properties(such as delivery address) and the values for those are stored in the Customer entity. For example my Order has a property called delivery address which is equal to Customer.Address. I need to keep them separated because the users have to be able to manipulate them separately. Thanks very much in advance
Susan
|
|
|
It sounds like either the Customer and the Order are in different units of work, or (perhaps more likely) GetCustomerByGuid is creating a new Customer object with the same GUID rather than retrieving the existing one. You need to change GetCustomerByGuid to return an existing Customer which is part of the current unit of work (the one you're adding the Order to). Once you do this, the Customer will not be inserted when you save the Order, because either it's already been inserted (if you're reusing the same UOW) or it's cleanly retrieved from the database (if you're using a new UOW). |
|
|
Incidentally, another option is to add the Customer and their Orders together, and save them in a single go: uow.Add(customer); Or using the reverse association: uow.Add(customer); Note you do not need to call IUnitOfWork.Add for the Order entities in this case because they automatically acquire a UOW from the customer association. |
|