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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:57:54+00:00 2026-06-05T15:57:54+00:00

We are using Entity Framework Code First with Foreign Key relationships. We investigating on

  • 0

We are using Entity Framework Code First with Foreign Key relationships. We investigating on ways on handling removing objects from an entities ICollection in our application.

When we have an entity with child relationships we can add objects directly to their ICollection using Add method. Now when you use remove you get the error

System.InvalidOperationException occurred Message=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.

I understand this is because Remove on the collection only deletes the relationship by nulling the foreign key. We wanted to write our business logic in our entity and allow removal.

So get root entity out out of it’s Repostiory e.g Order from OrderRepository then call some specific method of the entity e.g. Order.AddOrderline(Orderline orderline) This adds an OrderLine to the Orders virtual ICollection<OrderLine> OrderLines

However we can’t write code like Order.CancelOrderline(int orderLineId) because simply removing from the ICollection causes an error on savings changes.

There doesn’t seem to be anyway to achieve this by just manipulating the object collections. Obviously we can remove directly from Context. However I’d like to make it part of the entity. Can we clean up certain entities with no foreign key on SaveChanges event of Entity Framework? Obviously need to tell EF what entities can be removed if they have null foreign key.

We presently are using a repository pattern so the controller doesn’t have access to the context. I could obviously use an OrderLine repository or a remove OrderLine method on the Order repository. However just wondering if it was possible to write the code on the entity without references to the persistence mechanism.

Thoughts? Are we going about this all wrong? Do other ORMs allow you to just remove from Child Collections?

  • 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-05T15:57:56+00:00Added an answer on June 5, 2026 at 3:57 pm

    I don’t know if the following is a solution for you but Entity Framework supports Identifying Relationships. In such a relationship the foreign key of the child entity (dependent) to the parent (principal) must be part of the (composite) primary key of the child entity. For example – with DbContext data annotations – your model classes have to look like this:

    public class Order
    {
        [Key]
        public int OrderId { get; set; }
    
        public ICollection<OrderLine> OrderLines { get; set; }
    }
    
    public class OrderLine
    {
        [Key, ForeignKey("Order"), Column(Order = 1)]
        public int OrderId { get; set; }
    
        [Key, Column(Order = 2)]
        public int OrderLineId { get; set; }
    
        public Order Order { get; set; }
    }
    

    You can make the OrderLineId an autogenerated identity if you want. Important is only that the FK to Order is part of the PK.

    A code like this for example…

    using (var ctx = new MyContext())
    {
        var order = ctx.Orders.Include("OrderLines").Single(o => o.OrderId == 1);
        var orderLineToDelete = order.OrderLines
            .FirstOrDefault(ol => ol.OrderLineId == 5);
        if (orderLineToDelete != null)
            order.OrderLines.Remove(orderLineToDelete);
    
        ctx.SaveChanges();
    }
    

    …would indeed delete the orderLineToDelete from the database.

    More details are here in section “Considerations for Identifying and Non-identifying Relationships“.

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

Sidebar

Related Questions

When using Entity Framework Code First 4.3.1 it is possible to create relationships with
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I am using Entity Framework Code First. I want to query entites from database
Using Entity Framework 4.1 code first and POCO entities in MVC3; I would like
I'm using Entity Framework code first and pulling some data back from our database.
Using Entity Framework Code First CTP5, how do I create a primary key column
I need to map a many-to-many relationship using Entity Framework Code First. Its a
Hello everyone my problem is very simple. Using entity framework code first I want
(USING ENTITY FRAMEWORK 4.3 CODE FIRST) The code is below. I want to actually
I am Using C# 2010 Entity framework code first If I have A class

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.