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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:45:50+00:00 2026-05-29T09:45:50+00:00

EF 4.1 Database First approach. Say I have this table schema Users 1—M UserRoles

  • 0

EF 4.1 Database First approach.

Say I have this table schema

Users 1—M UserRoles M—1 Roles

Cascade delete is setup in the Foreign Keys

The UserRoles table has additional columns like CreatedDate so I create a model for UserRoles and map accordingly.

I end up with the following Models:

User
----
int Id
string Name
List<UserRoles> UserRoles

UserRoles
---------
int UserId
int RoleId
DateTime CreatedDate
User User
Role Role

Role
----
int Id
string Name
List<UserRoles> UserRoles

If I have my configuration correct, should I be able to delete a user and will the user roles rows be deleted WITHOUT having to clear the UserRoles collection manually?

So can I just do this:

DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();

Or do I HAVE to do this:

user.UserRoles.Clear();
DbContext.Entry(user).State = EntityState.Deleted;
DbContext.SaveChanges();

My testing shows I HAVE to clear the child collection, but I find conflicting information that if I have cascade delete setup correctly it should work by only deleting the User.

When I DON’T clear the UserRoles I receive this error:

The relationship could not be changed because one or more of the
foreign-key properties is non-nullable

Thanks for you help in clarifying this!

  • 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-05-29T09:45:51+00:00Added an answer on May 29, 2026 at 9:45 am

    You must use

    DbContext.Users.Remove(user);
    

    It is not the same thing as setting the state to Deleted. Setting the state won’t mark any child objects with cascading delete setup as Deleted but Remove will do.

    Setting the state to Deleted should work IF no children are loaded into the context because EF will send only a DELETE statement for the parent to the database and the database will delete the children as well due to the cascading delete in the database.

    IF however you have loaded children into the context setting the state on the parent to Deleted won’t set the state of the children. EF will throw the exception, it’s not the database who complains.

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

Sidebar

Related Questions

Lets say I have table like this: String | Int1 | Int2 "foo" 5
I have got a Database like this: users -id user_cars -user_id -car_id cars -id
I've always taken the approach of first deploying the database with a minimal set
As usual, some background information first: Database A (Access database) - Holds a table
I would like to make a First Access Database Setup Process in my spring
Let's say I have a table named products and I want to know how
I have two distinct sets of users in a database. I want to track
I've built an MVC3 application using the Entity Framework database first approach. I was
Consider, please this setup for a database backed application. ( in my case DB
Let's say I have a messaging service between users, with public and private messages.

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.