MVVM Row keeps getting inserted and not UPDATED

Hello all, Still GREEN when it comes to MVVM:

I have a row that needs to be updated. NOW... when I attempt to do the update it just inserts a new row

// the code needs to be updated where the ATPCodeID= ATPCodeID and NOT insert new rows.. grrrrrr
    public override void SaveChanges()
        {
            ATPCode code = (from c in Db.ATPCodes
                            where c.ATPCodeID == Request.ATPCodeID
                            select c).First();

            code.DisplayText = Request.DisplayText;


            if (Request.ActiveFlag.Value == false)
            {
                code.InactivationDateTime = Dt.CurrentDateTime;
                code.InactivationUserID = Request.Requestor.UserID;

            }

            else
            {
                code.InactivationDateTime = null;
                code.InactivationUserID = null;
            }
        
            code.ModifiedUserID = Request.Requestor.UserID;
            code.ModifiedDateTime = Dt.CurrentDateTime;
                        
            Db.ATPCodes.Add(code);
            Db.SaveChanges();

        }

Open in new window

HawaiiDragonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HawaiiDragonAuthor Commented:
public override void SaveChanges()
        {
            database = Db.ATPCodes.Where(a => a.ATPCodeID == Request.ATPCodeID).FirstOrDefault();
            database.DisplayText = Request.DisplayText;
            database.ModifiedUserID = Request.Requestor.UserID;
            database.ModifiedDateTime = Dt.CurrentDateTime;
            if (Request.ActiveFlag.Value == false)
            {
                database.InactivationDateTime = Dt.CurrentDateTime;
                database.InactivationUserID = Request.Requestor.UserID;

            }

            else
            {
                database.InactivationDateTime = null;
                database.InactivationUserID = null;
            }


            Db.SaveChanges();
           
                     
        }
Fernando SotoRetiredCommented:
Hi HawaiiDragon;

Do not know if you are still having issue with this but to your statement, "the code needs to be updated where the ATPCodeID= ATPCodeID and NOT insert new rows.. ", the reason why it is being inserted is because of this statement in your code.

 Db.ATPCodes.Add(code);

You are telling the Context to insert it and not to update the data that you got back from the query. If you remove that line of code then the Context will see it has a updated record and send the changes to the database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.