c# - 首先通过代码更新具有一对一关系的 ViewModel

标签 c# entity-framework ef-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();

    }

关于c# - 首先通过代码更新具有一对一关系的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450168/

相关文章:

c# - 旋转和打印正方形

c# - 如何检查查询是否通过网络执行?

c# - EF Core 2.0 中的一个实体 2 个表

c# - EF Code First 处理多对多关系

c# - FirstOrDefault 抛出 'Sequence contains more than one matching element'

c# - 获取表单的父表单名称

c# - Startup 类是如何发挥作用的?

c# - 为什么 Process.Start(ProcessStartInfo) 会失败?

c# - Entity Framework 简单名称会导致某些项目出现问题,但不会导致其他项目出现问题

c# - 使用 Entity Framework 6.1 和 MVC 5 从数据库使用 Code First 后如何同步模型?