Updating entity does not work in entity framework 6

entity-framework entity-framework-6

Question

I have migrated my project from EF4 to EF6. I am facing trouble while updating the records. Below is my code

public int SaveApplicantData(Applicant objApplicant, bool isEdit)
    {
        DBEntities context = new DBEntities();
        int applicantId = 0;
        try
        {

                if (objApplicant.Id > 0)
                {
                  var applicant = context.Applicants.Where(a => ((a.Id != objApplicant.Id) && (a.SSN == objApplicant.SSN))).FirstOrDefault();
                    if (applicant != null)
                    {
                        return -1;
                    }
                    else
                    {
                        applicant = context.Applicants.Find(objApplicant.Id);
                    }
                   if (applicant != null)
                    {
                        applicant.FirstName = "TEST NAME";
                        context.SaveChanges();
                    }
                }
                return objApplicant.Id;
            }
        }

I have also tried context.Entry(applicant).CurrentValues.SetValues(objApplicant) but does not work for me. Spending lot of hours in finding solution. Please assists with appropriate solution.

UPDATE

Also I tried

  context.Applicants.Attach(objApplicant);
  context.Entry(objApplicant).State = EntityState.Modified;
  context.SaveChanges();

but no success!

1
1
4/9/2015 10:36:07 AM

Popular Answer

Try the below code instead of adding in context.Applicants.Add(objApplicant);

 context.Entry(objApplicant).State = EntityState.Modified;
 context.SaveChanges();

Update: Try this code block.

                if (applicant != null)
                {
                    applicant.FirstName = "TEST NAME";
                    context.Entry(applicant).State = EntityState.Modified;
                    context.SaveChanges();
                }
0
4/9/2015 10:28:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow