我有 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();
}
关于c# - 首先通过代码更新具有一对一关系的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450168/