Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Working with Relationships > Removing a Relationship Between Objects

Removing a Relationship Between Objects

Let’s say that Dave is no longer the primary contact for Dave’s Dump. In fact, the service is so bad at this lodging that they no longer have a contact at all. This means we simply want to remove the relationship rather than changing it to a new Person.

To remove a relationship, you can remove the target object from a collection navigation property. Alternatively, you can set a reference navigation property to null. If your classes expose a nullable foreign key property for the relationship, a third option is to set the foreign key to null.

Warning

Removing relationships by changing the foreign key is only possible with nullable properties (for example, the PrimaryContactId property of the Lodging class). If your foreign key is an int (for example, the DestinationId property of the Lodging class), you won’t be able to set the value to null and by convention, that relationship would be required. Setting the value to 0 would cause a primary key/foreign key constraint error in the database, as there will be no parent whose primary key is equal to 0.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial