首先按代码更新ViewModel与一对一的关系

时间:2015-10-31 09:45:46

标签: c# entity-framework ef-code-first

我有两张一对一的关系表:

public class PersonCall
{
    public PersonCall()
    {
        Destination = new Destination();
    }
    [Key, ForeignKey("Destination")]
    public int DestinationId { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public string Job { get; set; }

    public virtual Destination Destination { get; set; }

}
public partial class Destination
{
    public int DestinationId { get; set; }

    public int? ActivityId { get; set; }

    [StringLength(50)]
    public string ActivityTextPersian { get; set; }

    public int Number { get; set; }

    public virtual Activity Activity { get; set; }

    public virtual PersonCall PersonCall { get; set; }
}

和这样的PersonCallViewModel:

    public class PersonCallViewModel
    {
    public int DestinationId { get; set; }

    [Required(ErrorMessage = "پر کردن این فیلد الزامی است")]
    [Display(Name = "نام")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "پر کردن این فیلد الزامی است")]
    [Display(Name = "نام خانوادگی")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "پر کردن این فیلد الزامی است")]
    [Display(Name = "سمت")]
    public string Job { get; set; }

    [Required(ErrorMessage = "پر کردن این فیلد الزامی است")]
    [Display(Name = "شماره پیجر")]
    public int Pager { get; set; }

}

它是PersonCallController中的PersonUpdate操作:

 [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult PersonUpdate([DataSourceRequest] DataSourceRequest request, PersonCallViewModel personCall)
    {
        if (personCall != null && ModelState.IsValid)
        {
            personCallService.Update(personCall);
        }

        var updateEntity = Json(new[] { personCall }.ToDataSourceResult(request, ModelState)); ;

        return updateEntity;
    }

我的问题是在PersonCallViewModel中更新 Pager 属性时,数据库没有更新它! 事实上,我的网格已更新,但当我刷新页面或在数据库中查看我的表行时,我的值回滚到之前的值。 这是我的代码:

 public void Update(PersonCallViewModel personCall)
    {
        var entity = new PersonCall();


        entity.DestinationId = personCall.DestinationId;
        entity.Destination.DestinationId = personCall.DestinationId;
        entity.FirstName = personCall.FirstName;
        entity.LastName = personCall.LastName;
        entity.Job = personCall.Job;
        entity.Destination.Number = personCall.Pager;


        entities.PersonCall.Attach(entity);
        entities.Entry(entity).State = EntityState.Modified;
        entities.SaveChanges();
    }
你帮我吗?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题!感谢@Henk Holterman的帮助!

我在Update方法中创建了一个Destination对象,并对其进行了更改,如下所示:

public void Update(PersonCallViewModel personCall)
    {
        var entity = new PersonCall();
        var entity2 = new Destination();

        entity.DestinationId = personCall.DestinationId;
        entity.Destination.DestinationId = personCall.DestinationId;
        entity.FirstName = personCall.FirstName;
        entity.LastName = personCall.LastName;
        entity.Job = personCall.Job;

        entities.PersonCall.Attach(entity);

        var equalDestination = entities.Destination.Where(pd => pd.DestinationId == entity.DestinationId);

        foreach (var item in equalDestination)
        {
            item.Number = personCall.Pager;
        }

        entity2 = equalDestination.FirstOrDefault();

        entities.Destination.Attach(entity2);


        entities.Entry(entity).State = EntityState.Modified;
        entities.Entry(entity2).State = EntityState.Modified;


        entities.SaveChanges();

    }