[英]Update ViewModel with one-to-one relationship by code first
我有2個表與一對一的關系:
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();
}
你幫我嗎?
我解決了這個問題! 感謝@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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.