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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:06:22+00:00 2026-05-31T00:06:22+00:00

I’ve got the below model. I am trying to delete a given productType associated

  • 0

I’ve got the below model.

enter image description here

I am trying to delete a given productType associated with an ActivityType; But when I try to instigate a delete via my generic methods, its not only deleting the relationship but its trying to delete the ProductType as well!! with a primary key of Zero in the where clause causing the erorr

“Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.”

code to delete below;

public void DeleteEntityAndSaveChanges<T, T1>(T entity, List<T1> subEntities)
            where T : class
            where T1 : class
        {
            Set<T>().Attach(entity);
            DeleteEntitiesAndSaveChanges(subEntities);
        }

public void DeleteEntitiesAndSaveChanges<T>(List<T> entities) where T : class
        {
            foreach (var entity in entities)
            {
                Set<T>().Attach(entity);
                Set<T>().Remove(entity);
            }

            SaveChanges();
        }

Usage:

DbContext.DeleteEntityAndSaveChanges(request.ActivityType, request.ActivityType.ProductTypes);

This is the sql thats being generated:

exec sp_executesql N'delete [dbo].[ProductTypeActivityTypes]
where (([ProductType_ProductTypeId] = @0) and ([ActivityType_EntityObjectId] = @1))',N'@0 bigint,@1 bigint',@0=1,@1=20

This is the offending SQL that dont want sent out, but is being generated by EF:

exec sp_executesql N’delete [dbo].[ProductTypes]
where (([ProductTypeId] = @0) and [TimeStamp] is null)’,N’@0 bigint’,@0=1

any ideas as to how I can get it to only delete the relationship?

Cheers.

  • 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-31T00:06:23+00:00Added an answer on May 31, 2026 at 12:06 am

    When you call Remove on the DbSet, this means delete that entity from the database when SaveChanges is called. As I understand it, what you really want to do is remove the association between the two entities. In general you do this by removing one entity from the collection navigation property of the other entity. For example:

    product.Activities.Remove(activity);
    

    But there is a wrinkle here. In your sample code you are calling Attach to attach both types of entities to the context. Calling Attach will not setup or restore any relationships between the entities. This is because, for many-to-many relationships, there is no FK in the entity to provide information about how it is related to other entities. The FKs are handled by the join table, which is not exposed in an EF many-to-many mapping.

    There are a couple of approaches to dealing with this. First, if possible and sensible for your architecture, you can let the EF context track the entities from the time they are queried until the time you call SaveChanges. This way EF will keep track of the relationships for you, including keeping track of deletes.

    Second, if the entities need to be attached to a new context, then you will need to keep track of the relationships such that those can be restored as well. There are a few ways to restore the relationships. One way is to build the graph of related entities before calling Attach. EF will then traverse and attach the entire graph, including relationships, when Attach is called. For example:

    // Restore the graph
    product.Activities.Add(activity1);
    product.Activities.Add(activity2);
    context.Products.Attach(product);
    
    // Delete the relationship
    product.Activities.Remove(activity1);
    context.SaveChanges();
    

    (I’m not using the generic methods you have just to make the code a bit clearer. It should work the same way with generics.)

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.