Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:28:41+00:00 2026-06-04T19:28:41+00:00

I have an optional relationship from a Child to a Parent class. I would

  • 0

I have an optional relationship from a Child to a Parent class. I would like to get an exception on SubmitChanges when the parent object is marked for deletion if there are still children around that reference it.

The configuration I’ve tried is this (there is no navigation property from the parent to the children):

modelBuilder.Entity<Child>()
    .HasOptional<Parent>(child => child.Parent)
    .WithMany()
    .HasForeignKey(child => child.ParentId)
    .WillCascadeOnDelete(false);

Like this EF sets the children’s ParentId property to null when deleting the parent, which is not what I want.

It works if the relationship is configured as required:

modelBuilder.Entity<Child>()
    .HasRequired<Parent>(child => child.Parent)
    .WithMany()
    .HasForeignKey(child => child.ParentId)
    .WillCascadeOnDelete(false);

This throws an exception, which would be the desired behaviour. But the relationship has to be optional. Is this possible with EF 4.3.1 using Code First?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:28:44+00:00Added an answer on June 4, 2026 at 7:28 pm

    No. That is the difference between optional and required. Required = must have a principal record and if you delete the principal record without cascading you will get an exception. Optional = doesn’t need a principal record and if you delete the principal record without cascading the FK is set to null.

    If you need anything else you must handle it yourselves.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like a JavaScript function to have optional arguments which I set a
I'd like to write a function that would have some optional code to be
Is it possible for a managed object to have a relationship with 'optional' unchecked?
I have a to-many (optional) relationship from entity A to entity B via the
I have one issue with retrieval Parent/Child relationship of type Folder Hierarchy Ideally, in
I have an entity Product which has relationship category, which is optional, and use
I use VB to get data through my form. I have some optional fields
I have a container class which uses boost::optional to hold the value. Here is
I have an object model where I'm checking a filter value of nested child
Say I have a customer table with 1:0..1 relationship with another table, I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.