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

  • Home
  • SEARCH
  • 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 5968875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:09:05+00:00 2026-05-22T20:09:05+00:00

I am using ASP.NET MVC2 in Visual Studio 2008. I believe the SQL Server

  • 0

I am using ASP.NET MVC2 in Visual Studio 2008. I believe the SQL Server is 2005. I am using Entity Framework to access the database.

I’ve got the following table with a composite primary key based upon iRequest and sCode:

RequestbyCount
    iRequest  integer
    sCode     varchar(10)
    iCount    integer

iRequest is a foreign key to a list of requests.

When a request is updated, I want to clear out the existing RequestbyCounts for that request and then add in the new RequestbyCounts. More than likely, the only difference between the old rows will be the Count.

For my code, I attempt it as follows:

//delete ALL our old requests
var oldEquipList = (from eq in myDB.dbEquipmentRequestedbyCountSet
                    where eq.iRequestID == oldData.iRequestID
                    select eq).ToList();
foreach (var oldEquip in oldEquipList)
{
     myDB.DeleteObject(oldEquip);
}

//  myDB.SaveChanges();    <---- adding this line makes it work

//add in our new requests
foreach (var equip in newData.RequestList)   //newData.RequestList is a List object
{
   if (equip.iCount > 0)
   {
    //add in our actual request items
      RequestbyCount reqEquip = new RequestbyCount();
      reqEquip.sCodePrefix = equip.sCodePrefix;
      reqEquip.UserRequest = newRequest;
      reqEquip.iCount = equip.iCount;
      myDB.AddToRequestbyCount(reqEquip);
   }
}

myDB.SaveChanges();   //save our results

The issue is when I run it with the intermediate SaveChanges line uncommented, it works as desired. But my understanding is that doing this breaks the transaction apart.

If I leave the intermediate SaveChanges commented out as above, the process fails and I receive a

Violation of PRIMARY KEY constraint
‘PK_RequestbyCount’. Cannot insert
duplicate key in object
‘dbo.RequestbyCount’.\r\nThe statement
has been terminated.

Obviously, without doing the intermediate SaveChanges, the old rows are NOT removed as desired.

I do NOT want the results saved unless everything succeeds.

I would rather not take the following approach:

//add in our new requests
foreach (var equip in newData.RequestList)
{
   if (equip.iCount > 0) && (**it isn't in the database**)
   {
    //add in our actual request items
      RequestbyCount reqEquip = new RequestbyCount();
      reqEquip.sCodePrefix = equip.sCodePrefix;
      reqEquip.UserRequest = newRequest;
      reqEquip.iCount = equip.iCount;
      myDB.AddToRequestbyCount(reqEquip);
   } else if (**it is in the database**) && (equip.iCount == 0) {
      **remove from database**
   } else {
      **edit the value in the database**
   }
}

Am I stuck doing the above code that basically makes a bunch of little calls to the database to check if an item exists?

Or is there some method that tell the framework to attempt to delete the rows I want but rollback if there is a failure inserting the new rows?

  • 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-22T20:09:06+00:00Added an answer on May 22, 2026 at 8:09 pm

    You don’t appear to be using transactions at all. You need to wrap all your code in

    using (TransactionScope transaction = new TransactionScope())
    {
        ...
        transaction.Complete();
    }
    

    Even better

    using (TransactionScope transaction = new TransactionScope())
    {
        try
        { 
           your code 
           transaction.Complete();
        }
        catch(Exception)
        {
           // handle error
        }
    }
    

    Using the try/catch block will ensure that the transaction is not committed if an exception occurs, which is what you stated you wanted.

    Lot’s more on entity framework transactions at Microsoft’s web site. The explanations there are quite good.

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

Sidebar

Related Questions

Is there anything similar to JavaDocs for ASP.NET MVC2 using the Visual Studio 2010
I'm using ASP.NET, with session state stored out of process in SQL Server. When
I am using ASP.NET MVC 2 in Visual Studio 2010. In my project I
I am using ASP.NET MVC 2 with Visual Studio 2010. A lot of my
I'm using ASP.NET MVC2 and C#, but this question applies to ASP.NET in general.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using asp.net, I need to generate a snapshot of an youtube video. I have
Using ASP.net 2.0, how do I present the information to the user similar to
Using ASP.net, how do you write ANSI characters to a text file? I need
Using asp.net MVC I'd like to do this inside a view: <%= Html.TextBox(textbox1, null,

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.