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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:24:35+00:00 2026-06-13T09:24:35+00:00

I have this class ( Simplified ) public class User { public string Username

  • 0

I have this class ( Simplified )

public class User {
    public string Username { get; set; }
    public virtual Section Section { get; set; }
}

public class Section {
    public int DepartmentNumber { get; set; }
    public virtual ICollection<User> DepartmentMembers { get; set; }
}

What i try to achive is to delete a section. All users related to this section shoulde get a null value. This dosent work now because REFERENCE constraint “FK_Users_Sections_Section”.

What woulde be the correct way to do this? woulde i have to remove all users from this section before deleteing it? Is there a more elegant way to do this? Im using EntityFramework.

My fluent API for this field:

modelBuilder.Entity<User>()
            .HasOptional(u => u.Section)
            .WithMany(s => s.DepartmentMembers)
            .Map(m =>
                m.MapKey("Section")
            )
            .WillCascadeOnDelete(false);

The WillCascadeOnDelete i have tried it by setting to false, removing the line and also adding the line with no arguments.

I guess the solution is quit simple but i cant find any good explaination ( or mabye i dont understand the explainations i have been looking at. )

  • 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-13T09:24:37+00:00Added an answer on June 13, 2026 at 9:24 am

    Although SQL Server has a SET NULL option for cascade deletes, which sets all foreign keys referencing a deleted record to null, Entity Framework does not use it.

    You can either set this option on the constraint yourself in SQL Server, or you can let Entity Framework take care of it by updating loaded entities.

    If you want EntityFramework to do it, you need to make sure the DepartmentMembers collection is loaded, so that all the User objects that will need to be updated are in the context.

    Section s = context.Sections.Include(s => s.DepartmentMembers).First();
    context.Delete(s);
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this (simplified) class: public class StarBuildParams { public int BaseNo { get;
i have this class public class Image { public string url { get; set;
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have simplified my code to this internal class Program { private static void
I have this class: Class B { private String D; private String E; }
I have this class which i would like to map: public class Contract {
I have this class: class OriginalClass { string FirstItem; List<string> ListOfSecondItems; } I want
I have the following POCO's: [PetaPoco.TableName(Customer)] [PetaPoco.PrimaryKey(ID)] public class User { [Required(ErrorMessage = Please
I have a model like this defined... public class Product{ private long timestamp; //
My simplified domain model looks something like this: public abstract class Entity<IdK> { public

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.