Iti Tyagi
Iti Tyagi

Reputation: 3661

Saving changes of Entity Framework in Asp.Net

I have created an entity Appraiser and there are methods to select values, display data etc. Now I want to save the changes made after data is displayed, I have a button named SAVE, which will be used to save changes. I am not able to get how to save the changes of this Entity?

Entity name is Appraiser, and I have created methods like get AppriaserDetails etc in DAL, BL and used them in aspx.cs.

This is my code:

 public void UpdateData(Appraiser appId)
    {

        var Appsave = context.Appraisers.FirstOrDefault(App => App.AppraiserId == appId.AppraiserId);
        Appsave.AppraiserName = appId.AppraiserName;
        Appsave.AppraiserPhones = appId.AppraiserPhones;
        Appsave.AppraiserAppraiserCompanyId = appId.AppraiserAppraiserCompanyId;
        Appsave.Address = appId.Address;
        Appsave.City = appId.City;
        Appsave.ProvinceState = appId.ProvinceState;
        Appsave.Email = appId.Email;
        context.SaveChanges();
    }

Upvotes: 0

Views: 588

Answers (1)

Developer
Developer

Reputation: 769

If u want to insert new record, then can use

        MyContext.Appraisers.AddObject(appraiserEntityObject);
        MyContext.SaveChanges();

In case of update

        if (appraiserEntityObject.EntityState == EntityState.Detached)
        {
            // In case of web, we got an existing record back from the browser. That                object is not attached to the context yet.
            MyContext.Appraisers.Attach(appraiserEntityObject);
            MyContext.ObjectStateManager.ChangeObjectState(appraiserEntityObject,  EntityState.Modified);
        }
        MyContext.SaveChanges();

Here MyContext is ur ObjectContext

Upvotes: 1

Related Questions