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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:23:55+00:00 2026-05-22T17:23:55+00:00

I am trying to remove a reference to an Entity in a One-To-Many relationship

  • 0

I am trying to remove a reference to an Entity in a One-To-Many relationship in the following manner, but am receiving an error when I try to attach the object ‘o’ to my DbContext. The error is:

“Adding a relationship with an entity which is in the Deleted state is not allowed.”

I have also tried the following in place of setting the EntityState:

 _db.OrganizationMetrics.Remove(om)

What is the right way to remove this?

<HttpPost()>
Function Edit(ByVal ovm As OrganizationViewModel)

    Dim o As Organization


    o = AutoMapper.Mapper.Map(Of OrganizationViewModel, Organization)(ovm)

    For Each om In o.OrganizationMetrics
        _db.OrganizationMetrics.Attach(om)

        If om.Value = "removeMe" Then
            _db.Entry(om).State = EntityState.Deleted
        ElseIf om.Id = 0 Then
            _db.Entry(om).State = EntityState.Added
        Else
            _db.Entry(om).State = EntityState.Modified
        End If
    Next

    _db.Organizations.Attach(o) 'Error is thrown here

    If (ModelState.IsValid) Then

        _db.Entry(o).State = EntityState.Modified
        _db.SaveChanges()

        Return RedirectToAction("Index")
    Else

        Return View(ovm)
    End If

End Function

UPDATE:

This is my now functioning code. The key is to not map the children entities back to the parent entity model from the view model, so that I can deal with them individually.

    <HttpPost()>
    Function Edit(ByVal ovm As OrganizationViewModel)

        Dim o As Organization

        o = AutoMapper.Mapper.Map(Of OrganizationViewModel, Organization)(ovm) //The Automapper code ignores the OrganizationMetrics members
        _db.Organizations.Attach(o)

        For Each om In ovm.OrganizationMetrics
            _db.OrganizationMetrics.Attach(om)

            If om.Value = "removeMe" Then
                _db.Entry(om).State = EntityState.Deleted
            ElseIf om.Id = 0 Then
                _db.Entry(om).State = EntityState.Added
            Else
                _db.Entry(om).State = EntityState.Modified
            End If
        Next


        If (ModelState.IsValid) Then

            _db.Entry(o).State = EntityState.Modified
            _db.SaveChanges()

            Return RedirectToAction("Index")
        Else

            Return View(ovm)
        End If

    End Function
  • 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-22T17:23:56+00:00Added an answer on May 22, 2026 at 5:23 pm

    When you attach o with _db.Organizations.Attach(o), it goes through all its children and finds that some of them are deleted. When it tries to attach them, you get the error you’re showing. It makes perfect sense.

    Step back and figure out what it is you want to do. The easiest way to delete something is to get it and then delete it. Something like:

    context.DeleteObject(context.MyEntity.Single(r => r.Id == myId));
    

    If you want, you can mock the MyEntity object with just its key and then delete that object, it will work as well and save you a select query.

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

Sidebar

Related Questions

I'm trying to make a one to many self reference using EF4.1 code first.
I'm trying to remove all but the first child component from a Java Container.
I am trying to remove a directory with rmdir , but I received the
I am trying to remove the attribute xmlns=http://webdev2003.test.com from the following xml using xsl/xslt,
I've mapped two entities using JPA (specifically Hibernate). Those entities have a one-to-many relationship
I have following kinds of classes for hibernate entity hierarchy. I am trying to
I am trying to remove Details from http://localhost:1985/Materials/Details/2/Steel but some how my route doesn't
I am trying to remove a component from list in R, but it is
While working on an existing project I suddenly got the following error when trying
I am having a problem trying to map out a many-to-many relationship , where

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.