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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:23:25+00:00 2026-06-17T18:23:25+00:00

I have this object: public class Profile { public int Id {get;set;} public List<User>

  • 0

I have this object:

public class Profile
{
     public int Id {get;set;}
     public List<User> Users {get;set;}
}

and I have a page to delete profiles.. so I want to delete a profile if there is not users related..

I tried this:

var profile = _db.Profile
.Include(p => p.Users)
.SingleOrDefault(p => p.Id == id);

_db.Profile.Remove(profile);
_db.SaveChanges();

but this automatically delete all users related(I don’t undestand why since I don’t set any especial configuration for that).

How can I avoid this?
How can I get a exception when I try to SaveChanges?

I would love to avoid checking the Users.Count directly in my code to do it.

  • 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-17T18:23:26+00:00Added an answer on June 17, 2026 at 6:23 pm

    you can use the extension method

    .WillCascadeOnDelete(false);
    

    for a specific relation,

    or disable all delete cascade with the OneToManyCascadeDeleteConvention

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
    
        {
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
       }
    

    This will show you some more details (EF 4 CTP5, but should be valuable)

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

Sidebar

Related Questions

Assume I have this domain object... public class SpansMultipleTables { public int CommonID {get;
I have this object: class a { public string Application; public DateTime From, To;
I'm just wondering .. I have and object like this public class Entry{ public
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have a model similar to this: public class myModel { public ClassA ObjectA
Let's say I have some model objects that resemble this: public class FooModel {
I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
I have a JPA object which has a many-to-many relationship like this: @Entity public
Here's the relevant code: Public class User.cs: public void FindByID(int id) { Parser parser
Let's say I have objects which look very roughly like this: class object {

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.