amir stack
amir stack

Reputation: 217

Update ViewModel with one-to-one relationship by code first

I have 2 table with one-to-one relationship:

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

and a PersonCallViewModel like this:

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

}

and it's PersonUpdate action in PersonCallController:

 [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;
    }

My problem is when update Pager property in PersonCallViewModel, database not updated it! in fact, my grid is updated but when I refresh page or see my table rows in database my value rollback to previous value. here is my code:

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

do you help me?

Upvotes: 3

Views: 171

Answers (1)

amir stack
amir stack

Reputation: 217

I solved this problem! thanks of @Henk Holterman for his help!

I create a new object of Destination in Update method and apply changes to that as below:

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

    }

Upvotes: 1

Related Questions