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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:55:58+00:00 2026-06-04T06:55:58+00:00

I have used EF to generate classes for my database tables: public partial class

  • 0

I have used EF to generate classes for my database tables:

public partial class Course
{
    public Course()
    {
        this.People = new HashSet<People>();
    }

    public int ID { get; set; }
    public string Name { get; set; }       

    public virtual ICollection<Person> People { get; private set; }     
}

public partial class Person
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual Course Course { get; set; }
}

As you can see each course has a collection of people. I created a second partial class so my code does not get cleared when i refresh the EF diagram. My question is how can i clear the list of people from within the course object?

I tried:

public partial class Course
{    
      public void ResetCourse()
      {
          this.People.Clear();
      }
}

But i get this error:

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.

which is apparently caused because EF is not actually deleting the person object just removing the relationship between the two which SQL throws out because it cannot have a null key.

Apparently i should use something like
context.DeleteObject(person)

However inside the course object it has no reference to the context and i wanted to keep the code inside the object to keep it simple of the ui code.

  • 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-04T06:55:58+00:00Added an answer on June 4, 2026 at 6:55 am

    Instead of using Course classes to manage database operations; it is better to employ repository and unitofwork patterns while working with entity framework. Otherwise, your entities always carries context on them; which will lead to some problems.

    Just create a Course repository with context as a parameter and employ the database operations in the repository instead of the entity itself. Refer to:
    Unit of Work and repository patterns

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

Sidebar

Related Questions

I have used hibernate reverse engineering to generate my model and Dao classes i
I have classes that are structured like the following: public class Forecast { [Key]
I have used xsd.exe tool to generate data class but wcf doesn't like the
I have used hibernate ant tool to generate hibernate pojo classes with annotations. What
I have used Visual Studio 2008 ASP.NET AccessDataSource Wizard to generate the update command.
I have the following method (used to generate friendly error messages in unit tests):
I have an XSD file which is used to generate some objects which are
I have some XML files which are used to generate my webpages, however I
I used SWIG to generate a Perl module for a C++ program. I have
I have implemented the sweep-line approach used by Domiter and Zalik to generate a

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.