实体框架更新实体

时间:2016-01-11 00:08:38

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

我正在尝试更新一个实体及其所有关系,我不确定这是否可行。如果不可能,最好的方法是什么?

我的ASP MVC控制器......

public ActionResult TimeSheetEdit(Timesheet timesheet, string submitType)
{
    _timesheetRepository.Update(timesheet);
    _timesheetRepository.Commit();

    return RedirectToAction("TimeSheetEdit", new {id=timesheet.TimeSheetId});
}

我的时间表模型有多个属性,一个属性是另一个实体(有一个实体列表)。我希望EF会遍历我的所有关系并更新所有内容,但它似乎只更新了Timesheet模型并忽略了所有其他相关实体。

这是正常还是我错过了什么?如果这是正常的,那么完成我想要做的事情的最佳做法是什么?

存储库更新代码...

    public void Update(T entity)
    {
        _dbSet.Attach(entity);
        _context.Entry(entity).State = EntityState.Modified;
    }

    public int Commit()
    {
        return _context.SaveChanges();
    }

我不确定它是否会有所帮助,但这是我的模型

时间表

public class Timesheet
    {
        [Key]
        public int TimeSheetId { get; set; }

        public Guid Id { get; set; }

        public DateTime StartDate { get; set; }

        public DateTime EndDate { get; set; }

        public TimeSheetStatus TimeSheetStatus { get; set; }

        public int EmployeeId { get; set; }

        public virtual Employee Employee { get; set; }

        public virtual List<TimesheetLine> TimesheetLines { get; set; }
    }  

TimesheetLine

public class TimesheetLine
{
    [Key]
    public int TimeSheetLineId { get; set; }

    public Guid CustomerId { get; set; }

    public string CustomerName { get; set; }

    public Guid EarningsId { get; set; }

    public virtual List<TimeSheetLineUnit> Units  { get; set; }

    public int TimeSheetId { get; set; }

    public virtual Timesheet Timesheet { get; set; }
}

TimeSheetLineUnit

public class TimeSheetLineUnit
{
    [Key]
    public int UnitId { get; set; }

    public decimal Hours { get; set; }

    [DisplayName("Notes")]
    public string Description { get; set; }

    public int Index { get; set; }

    public int TimeSheetLineId { get; set; }

    public virtual TimesheetLine TimesheetLine { get; set; }

}

3 个答案:

答案 0 :(得分:0)

为了解释我上面的评论,我建议可能是这个问题。

当将数据库作为模型返回给服务器时,MVC要求“Name”属性是唯一的。

我会循环查看这样的子记录:

@for (int i = 0; i < Model.Count; i++)
      {
        <tr>
          <td style="text-align: center;" class="Cell">
            @Html.CheckBoxFor(m => Model[i].Selected)
          </td>
          <td class="Cell">
            @Html.DisplayFor(m => Model[i].Name)
            @Html.HiddenFor(m => Model[i].Name)
          </td>
          <td class="Cell">
            @Html.DisplayFor(m => Model[i].AddressPhysical)
            @Html.HiddenFor(m => Model[i].AddressPhysical)
          </td>
          <td class="Cell">
            @Html.DisplayFor(m => Model[i].AddressPhysicalPostCode)
            @Html.HiddenFor(m => Model[i].AddressPhysicalPostCode)
          </td>
        </tr>
      }

注意:如果这样做,列表中的每个子行都将拥有自己唯一的“Name”属性,MVC将能够唯一地标识每一行数据,因此完整数据将正确传递回模型中的服务器。

这只是一个示例,但请尝试一下,看看你怎么做。

答案 1 :(得分:0)

对于Ef更新存储库,请勿使用attach 首先在你的通用存储库

中为你注入DbContext
  // my generic repository
  public class EntityBaseRepository<T>:IEntityBaseRepository<T> 
   where T :class , new()
 {
    //my context
    private DataContext dbContext;

     // use Dependency injection 
      public EntityBaseRepository(DataContext _dbContext)
   {
      this.dbContext = _dbContext;

   }


     //update method
       public void Update(T entity)
    {
        DbEntityEntry entry = this.dbContext.Entry(entity);
        entry.State = EntityState.Modified;
    }


 }

 // at the end call DbContext SaveChanges() from controller

答案 2 :(得分:-1)

请检查this

也许您需要以更新方式添加一些代码。

这样的事情:

SongSequences