Avatar of Member_2_1242703
Member_2_1242703
 asked on

Updating a single record in forEach using C#/Entity

Is there any way I can update a record after doing something in a for each? This is the idea (in bold), but doesn't work.

 using (var db = new PTO_SystemEntities())
            {
                var pto = db.PTO_REQUEST
                    .Where(q => q.Status != "Cancelled" && q.Status != "Processed" && q.Type != "Reverse Leave");
                foreach (var i in pto)
                {
                    Console.Write(i.EMPLID.Trim() + ", " + location.Trim() + ", " + jobcode.Trim() + ", " + i.Hours + Environment.NewLine);
[b]                    db.Entry(i).Property(e => e.Status).CurrentValue = "Processed";
                    db.Entry(i).Property(e => e.Status).IsModified = true;
                    db.SaveChanges();[/b]
                }
            }

Open in new window

C#.NET Programming* Entity Framework Core.NET MVC

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

Hi Mike;

You state the following, "This is the idea (in bold), but doesn't work.", how does it not work, do you get a run time error or compile error or something else?

Please also post the schema of the PTO_REQUEST table from the database.
Member_2_1242703

ASKER
This is the error I'm getting:

"New transaction is not allowed because there are other threads running in the session."

Open in new window

Fernando Soto

Please post the schema of the database table PTO_REQUEST. Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Member_2_1242703

ASKER
ID int (primary key, is identity)
Status VarChar (50)
Hours Decimal (5, 2)
EMPLID char(11)
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Member_2_1242703

ASKER
Yup, was just coming on to post my solution...

            using (var db = new PTO_SystemEntities())
            {
                var pos = db.PTO_REQUEST
                    .Where(q => q.Status != "Cancelled" && q.Status != "Processed" && q.Type != "Reverse Leave");
                foreach (var i in pos)
                {
		    //whatever code here
                    using (var dd = new PTO_SystemEntities())
                    {
                        dd.PTO_REQUEST
                            .Where(q => q.ID == i.ID)
                            .ToList()
                            .ForEach(a => a.Status = "Processed");
                        dd.SaveChanges();
                    }                                       
                }}

Open in new window

Fernando Soto

Very good Mike, have a great day.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.