如何使用Ef更新记录 [英] How Do I Update Record Using Ef

查看:73
本文介绍了如何使用Ef更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一条记录的Ef更新多个列值,但它没有给出任何错误,也没有更新

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

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);



如何使用EF更新多个字段? PLZ


how to update multiple field using EF?? PLZ

推荐答案

我严重建议您本书 [ ^ ]并完成它。您的代码证明了大量的概念,并且您不会在这样的论坛中学习它们。



您的代码没有完全没有任何意义,你的变量名需要重做,以使它们更有意义,更容易阅读和调试。



EF中的典型更新操作是这样的:

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();
}





我不知道你在整个附加事情上做了什么,但它看起来真的错了。 Attach通常用于告诉Entity Tracker开始跟踪它没有从数据库中获取的对象的更改,例如添加新对象。我很少在工作中使用它。



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.


这篇关于如何使用Ef更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆