Still running into this same problem, even in other situations. What if I wanted to do something like this...
Code:
Customer cust = new Customer();
cust.Name = "Acme Solutions";
Contact c1 = new Contact();
c1.Name = "John";
cust.AddContact(c1);
Contact c2 = new Contact();
c2.Name = "Sally";
cust.AddContact(c2);
So far, no problems. The customer is the parent, and the contacts are the children. Cascade="all-delete-orphan" will handle deleting the contacts when the parent customer is deleted. This is good.
Code:
Project p1 = new Project();
p1.Name = "Database Upgrade Project";
cust.AddProject(p1);
Again, no problems. Same type of relationship between customer and project, a parent-child with cascade="all-delete-orphan". A project cannot exist without its parent customer.
OK, now, I want to associate a contact with a project.
Code:
p1.PrimaryBillingContact = c1;
No problem. I can even remove the association by setting to null.
Code:
p1.PrimaryBillingContact = null;
But, I do want to assign a billing contact. So...
Code:
p1.PrimaryBillingContact = c1;
Now, here is where my problem comes into play. If I delete contact c1 from the customer's contact collection, I want the PrimaryBillingContact property for Project to be set back to null.
Code:
contactDao.Delete(c1);
Assert.IsNull(p1.PrimaryBillingContact)
But, I cannot figure out a mapping or cascading strategy to handle this effectively. I wanted to simply have PrimaryBillingContact map to a field in my Project table called PrimaryBillingContactID. But, this does not seem to work well when it comes to cascading.
Please... any suggestions?
Thanks.
Kevin