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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:27:53+00:00 2026-06-01T00:27:53+00:00

I writing a simple method which should delete an entity from the database if

  • 0

I writing a simple method which should delete an entity from the database if possible, else it should mark the record as inactive if there is an integrity rule stopping the record from being deleted.

The code I have is:

    public ActionResult Delete(int id)
    {
        try
        {
            Service.Repository<TEntity>().Delete(id);
            Service.SaveChanges(this.CurrentUser);
            return this.RedirectToActionPermanent("Index");
        }
        catch (DbUpdateException)
        {
            Service.Repository<TEntity>().D

            // Could not delete due to referential integrity so mark as inactive
            var obj = Service.Repository<TEntity>().Find(id);
            obj.Inactive = true;

            // Error thrown here as obj is already marked as deleted
            Service.SaveChanges(this.CurrentUser);

            return this.RedirectToActionPermanent("Index");
        }
    }

The issue I have is that after when I run the code within the DbUpdateException and then execute save, another exception is thrown as the entity is still marked as ‘deleted’. What is the best way of removing the original delete action from entity so I can try the save again?

  • Edit
    I meant to say that I have seen lots of people stating that I should dispose of the existing context and get a new one. To me this seems a little wrong in this context as I am injecting the context upon each Http request via Unity.

Thanks

  • 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-01T00:27:54+00:00Added an answer on June 1, 2026 at 12:27 am

    You can refresh the entity from database then modify it again. You have to add a method to you repository in your case.

    context.Refresh(RefreshMode.StoreWins, object);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple win forms app in C#. There is a method
I'm writing a semi-simple database wrapper class and want to have a fetching method
I'm writing a simple Google Web Toolkit service which acts as a proxy, which
I'm writing a simple program in Java which includes a KeyListener with the following
I am writing a SharePoint web part which will have a simple ASP.NET form.
I'm writing a simple app which allows a user to enter their income and
I'm writing a simple music player, and I've created a playback service which implements
I am writing one plugin using NPAPI. I was looking for simple example which
I am writing a simple web service using .NET, one method is used to
We have simple project which takes a number of messages from a number of

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.