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 8038631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:17:45+00:00 2026-06-05T03:17:45+00:00

For an application using Code First EF 5 beta I have: public class ParentObject

  • 0

For an application using Code First EF 5 beta I have:

public class ParentObject
{
    public int Id {get; set;}
    public virtual List<ChildObject> ChildObjects {get; set;}
    //Other members
}

and

public class ChildObject
{
    public int Id {get; set;}
    public int ParentObjectId {get; set;}
    //Other members
}

The relevant CRUD operations are performed by repositories, where necessary.

In

OnModelCreating(DbModelBuilder modelBuilder)

I have set them up:

modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects)
            .WithOptional()
            .HasForeignKey(c => c.ParentObjectId)
            .WillCascadeOnDelete();

So if a ParentObject is deleted, its ChildObjects are too.

However, if I run:

parentObject.ChildObjects.Clear();
_parentObjectRepository.SaveChanges(); //this repository uses the context

I get the exception:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

This makes sense as the definition of the entities includes the foreign key constraint which is being broken.

Can I configure the entity to “clear itself up” when it gets orphaned or must I manually remove these ChildObjects from the context (in this case using a ChildObjectRepository).

  • 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-05T03:17:46+00:00Added an answer on June 5, 2026 at 3:17 am

    It is actually supported but only when you use Identifying relation. It works with code first as well. You just need to define complex key for your ChildObject containing both Id and ParentObjectId:

    modelBuilder.Entity<ChildObject>()
                .HasKey(c => new {c.Id, c.ParentObjectId});
    

    Because defining such key will remove default convention for auto incremented Id you must redefine it manually:

    modelBuilder.Entity<ChildObject>()
                .Property(c => c.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    

    Now calling to parentObject.ChildObjects.Clear() deletes dependent objects.

    Btw. your relation mapping should use WithRequired to follow your real classes because if FK is not nullable, it is not optional:

    modelBuilder.Entity<ParentObject>().HasMany(p => p.ChildObjects)
                .WithRequired()
                .HasForeignKey(c => c.ParentObjectId)
                .WillCascadeOnDelete();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First
I'm creating an application where using the Entity framework using code-first. I have created
I am using mvc3 to develop an database application and followed the Code-First Tutorial
When my application first runs I'm using some simple code to read in some
I'm developing application using backbone.js & jquery. I have following code in model: runReport:
I'm creating a application using entity framework code-first and i'm facing some problems with
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I am using code first approach in a ASP.NET MVC 3 application and all
I've been using Code-First Migrations Beta up until now and when migrating my database
We're currently developing an application that is using EF Code first (EF June 2011

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.