Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to update a specific record in database by using entity framework

my code is following

C#
public int UpdateDataToDb(BusinessObjects objects, PersonalData newPersonal, EducationalData newEducational, ContactData newContact)
        {
            int status = 5;
            try
            {
                /// Updating Status
                UserCredential Credentials = newPersonEntity.UserCredentials
                                               .First(cd => cd.UserName == objects.UserName);
                Credentials.Status = objects.Status;

                /// Updating Data
                switch (objects.FormId)
                {
                    case 1:
                        PersonalData personal = newPersonEntity.PersonalDatas.First(dt=>dt.UserName==objects.UserName && dt.FormName==objects.FormName);
                      /// Suggest Code Here      
                    default:
                        break;
                }

                /// Saving Changes to Db
                return newPersonEntity.SaveChanges();

            }
            catch (Exception ex)
            {
                CatchError(ex);
                return 3;
            }



        }


i want to update it by object tell me the best way

thanks in advance
Posted

1 solution

It's fairly easy. This goes in your "/// Suggest Code Here" section.

C#
...
personal.PropertyToChange = newValue;
newPersonEntity.Entry(personal).State = EntityState.Modified;
...


When DbContext.SaveChanges() is called the new value will be stored.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900