Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to update multiple column value using Ef of one record but its does not give any error and there is no updating too
C#
Entities ef1 = new Entities();
                  int id1 = SafeConvert.ToInt16(ddlRollNo.SelectedValue);
                  var emp1 = (from em in ef1.ModifiedNames where em.Id == id select em).FirstOrDefault();
                  var Emp1 =(from em in ef1.Employees where em.Id == emp.BeltNo select em).FirstOrDefault();
                   CTP.HRMS.WebApp.EF.Employee Emp = new EF.Employee();
                   Emp.UserName = MyUser.UserName;
                   Emp.EntryDate = DateTime.Now;
                   Emp.Id =Emp.Id ;
                   Emp.CtrNo = txtCtrNo.Text;
                   Emp.Rank_Id = SafeConvert.ToByte(ddlRank.SelectedValue);
                   Emp.Name = txtName.Text;
                   Emp.Fathername = txtFatherName.Text;
                   Emp.Address = txtAddress.Text;
                   Emp.Height = txtHeight.Text;
                   Emp.Chest = txtChest.Text;
                   Emp.Caste = txtCaste.Text;
                   Emp.DistinguishMark = txtDistinguishMark.Text;
                   Emp.Cnic = txtCnic.Text;
                   Emp.SeniorityNo = SafeConvert.ToInt16(txtSeniorityNo.Text);
                   Emp.DateOfBirth = SafeConvert.ToDateTime(txtDateOfBirth.Text);
                   Emp.DateOfEnrollment = SafeConvert.ToDateTime(txtDateOfEnrollment.Text);
                   Emp.Education_Id = SafeConvert.ToByte(ddlEducation.SelectedValue);
                   Emp.Province_Id = SafeConvert.ToByte(ddlProvince.SelectedValue);
                   Emp.District_Id = SafeConvert.ToByte(ddlDistrict.SelectedValue);
                   Emp.EntryTestMarks = SafeConvert.ToDecimal(txtEntryTestMarks.Text);
                   Emp.CourceTestMarks = SafeConvert.ToDecimal(txtCourceTestMarks.Text);
                   Emp.CellNo = txtCellNo.Text;
                   Emp.MartialStatus_Id = SafeConvert.ToByte(ddlMartialStatus.SelectedValue);
                   Emp.OtherQualification = txtOtherQualification.Text;
                   Emp.IsEligibalForRouteDuty = txtIsEligibalForRouteDuty.Checked;
                   Emp.ImgBody = (Emp.ImgBody == null) ? new byte[0] : EmpImg.ImgBody;
                   //Image saving code
                   Byte[] imgByte = null;
                   if (txtEpPicture.HasFile && txtEpPicture.PostedFile != null)
                   {
                       //To create a PostedFile
                       HttpPostedFile File = txtEpPicture.PostedFile;
                       //Create byte Array with file len
                       imgByte = new Byte[File.ContentLength];
                       //force the control to load data in array
                       File.InputStream.Read(imgByte, 0, File.ContentLength);
                       Emp.ImgBody = imgByte;
                   }
                   ef1.Employees.Attach(ef1.Employees.Single(x => x.Id == Emp1.Id));

                   ef1.SaveChanges();
                   MsgLitteral.ShowNotificationInline("Employee Has Been Updated", true);

how to update multiple field using EF?? PLZ
Posted
Comments
Krunal Rohit 14-Nov-15 6:11am    
Did you try to use debugger ?

-KR

1 solution

I SERIOUSLY recommend you get this book[^] and work through it. There's a ton of concepts you're missing as evidenced by your code and you're not going to learn them in a forum like this.

Your code doesn't make any sense at all and your variable names need to be redone to make them meaningful and easier to read and debug.

A typical update operation in EF goes something like this:
using (var context = new MyDbContext())
{
    // Get the employee record to update
    Employee employee = context.Employees.Find(employeeId);

    // Update the properties of the employee
    employee.Property = some new value;
    ...

    // Save the changes
    context.Entry(employee).State = EntityState.Modified;
    context.SaveChanges();
}


I don't konw what you're doing with the whole "Attach" thing, but it looks really wrong. Attach is normally used to tell the Entity Tracker to start tracking changes to an object that it did NOT get from the database, like adding a new object. I rarely ever use it in my work.
 
Share this answer
 
Comments
Sajid227 16-Nov-15 0:29am    
thanx sir ,i am going to follow your lines.
Dave Kreskowiak 16-Nov-15 10:00am    
Don't. Get the book and read up on EF so you UNDERSTAND what the code is doing instead of relying on copy'n'paste code and praying that it works for you.
Sajid227 25-Nov-15 2:47am    
i have not read that book,but doing properly googling i have cleared up misunderstanding,done my job too.thanx again

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