实体框架不更新表

时间:2016-06-18 07:44:24

标签: asp.net asp.net-mvc entity-framework

我正在尝试更新我的表格。它会更改并返回到服务器,但它不会更新我的表。如果我在employeeInformation.APersonal.Name上编辑并在带有编辑名称的控制器上显示。但是在数据库表中没有变化。保持现有价值。请帮帮我。

   public class EmployeeInformation()
    {
    public Guid Id{set;get;)
    public int TrainingHistoryId{set;get;}
    public virtual TrainingHistory ATrainingHistory { set; get; }

    }

    public class TrainingHistory ()
    {
    public int id{set;get;)
    public string TrainingName{set;get;}
    public virtual IList<Training> Trainings{set;get;}
    public virtual IList<ProfessionalTraining> ProfessionalTrainings{set;get;}
    }
    public class ProfessionalTrainings()
    {
    public int Id{set;get;)
    public string Name{set;get;}
    public double Grade{set;get;}
    public string institute{set;get;}
    public virtual IList<TrainingHistory > TrainingHistories{set;get;}
    }
    public class Trainings()
    {
    public int id{set;get;}
     public string name{set;get;}
    public string PassingYear{set;get;}
     public virtual IList<TrainingHistory > TrainingHistories{set;get;}
}
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EmployeeEdit(EmployeeInformation employeeInformation)
                {
                    if (ModelState.IsValid)
                    {

                        var path = "";
                        if (employeeInformation.AFile.Picture!=null&&employeeInformation.AFile.Picture.ContentLength !=0)
                        {

                            path = Path.Combine(Server.MapPath("~/Images/Employee"), employeeInformation.AFile.FilePathCode + ".jpg");
                            employeeInformation.AFile.Picture.SaveAs(path);

                        }
                        var a=db.
                        db.Entry(employeeInformation).State = EntityState.Modified;
                        db.SaveChanges();
                        return RedirectToAction("EmployeeList");
                    }
                    return View(employeeInformation);
                }

在控制器中,员工信息具有所有必需值和修改值。但它没有更新价值。

1 个答案:

答案 0 :(得分:0)

如果添加新实体,则必须创建并将其添加到DbSet。

db.(your databasetablename).Add(entity);
db.SaveChanges();

指定实体的属性

var entity=db.(your databasetablename).where(f=>f.Id==??).SingleOrDefault();

如果更新存在的实体,则必须先从DbSet中找到它;

db.SaveChanges();

指定实体的属性

split