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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:38:32+00:00 2026-05-31T02:38:32+00:00

I am trying to simplify my code and am moving some of the core

  • 0

I am trying to simplify my code and am moving some of the core logic on to the Entity Framework entities themselves (Seems like that is why we have entities that model business logic). Presently entities are pretty much just a bunch of properties and collections.

I am looking at having a function on the entity that removes an item from a collection and adds it to another collection on the entity.

Now the add to the other collection function works perfectly. However the remove – removes the item from the collection however it doesn’t delete.

I get:

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

I understand that this is because removing from collection doesn’t actually mark for delete and when context save changes occurs it is upset by the key being nulled but not marked for removal. As I am not in a repository and have no access to the context within. How am I supposed to let the context know that this item needs to be deleted?

I thought I was supposed to make sure domain entities encapsulated appropriate business logic. Is this just the wrong thing to be doing? How can I get around this? Should I get around this?

If I use the extension method:

public static ObjectContext GetContext(this IEntityWithRelationships entity)
{
    if (entity == null)
        throw new ArgumentNullException("entity");

    var relationshipManager = entity.RelationshipManager;

    var relatedEnd = relationshipManager.GetAllRelatedEnds()
                                        .FirstOrDefault();

    if (relatedEnd == null)
        throw new Exception("No relationships found");

    var query = relatedEnd.CreateSourceQuery() as ObjectQuery;

    if (query == null)
        throw new Exception("The Entity is Detached");

    return query.Context;
}

I can access the context within the entity like this:

var context = this.GetContext() as Entities;
context.events.DeleteObject(event);

This seems hideous but does work. Surely doing this can’t be the right way?

  • 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-31T02:38:34+00:00Added an answer on May 31, 2026 at 2:38 am

    I changed the models to make the relationships between parent and child identities identifying relationships by using a composite key containing the parents key. This means that removing the entity from the child collection removes it without requiring access to the context. See this question Is it possible to remove child from collection and resolve issues on SaveChanges?

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

Sidebar

Related Questions

I am having some trouble trying to simplify some logic that is contained in
I'm trying to simplify some inherited PHP code that assigns a value to one
I'm trying to simplify some code where I have an extremely long switch statement
I'm trying to simplify some code by putting the ViewModel models into the code
I am trying to specialize some utility code on const member functions, but have
I'm trying to DRY up some code, and I feel like Ruby's variable assignment
I'm trying to implement the repository pattern using entity framework code first rc 1.
I have an IQueryable whose Entity Framework 4 objects I would like to project
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Hi i'm trying to process the mysql_fetch_array query below and simplify the code so

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.