通过实体框架6中的视图模型保存多对多关系

时间:2017-06-06 06:32:49

标签: entity-framework

我有两个具有多对多关系的实体(TaskStep和Role),我使用一个视图模型(名为StepViewModel)来显示taskstep,它允许的角色,StepViewModel是:

  public class StepViewModel
{
    public StepViewModel()
    {

    }
    public TaskStep TaskStep { get; set; }

    public virtual ICollection<AllowedRole> AllowedRole { get; set; }

}

我使用此方法显示taskstep:

 public static StepViewModel GetTaskStepData(int stepId)
    {
        using (var context = new WFContext())
        {

            return context.TaskSteps.Where(ts => ts.ID == stepId).Include(ts => ts.Roles)
                .Select(ts => new StepViewModel()
                {
                    AllowedRole = context.Roles.Select(r => new AllowedRole()
                    {
                        Role = r,
                        Allowed = ts.Roles.Select(x => x.ID).Contains(r.ID)
                    }).ToList(),
                    TaskStep = ts
                })
                .SingleOrDefault();
        }
    }

但是当我保存任务步骤时,它的角色更改无法保存。我的保存方法是:

public static void SaveTaskStep(StepViewModel stepViewModel)
    {
        using (var context = new WorkFlowContext())
        {

            //TaskStep taskStep = context.TaskSteps.Where(ts => ts.ID == stepViewModel.TaskStep.ID).Include(ts => ts.Roles).SingleOrDefault();

            TaskStep taskStep = stepViewModel.TaskStep;

            List<int> aRolesIDs = stepViewModel.AllowedRole.Where(ar => ar.Allowed == true).Select(ar => ar.Role.ID).ToList();
            List<Role> roles = context.Roles.Where(r => aRolesIDs.Contains(r.ID)).Include(r => r.AllowedTaskSteps).ToList();

            taskStep.Roles.Clear();
            foreach (Role role in roles)
            {
                if (context.Entry(role).State == EntityState.Detached)
                    context.Roles.Attach(role);

                taskStep.Roles.Add(role);
            }

            if (taskStep.ID == 0)
                context.TaskSteps.Add(taskStep);
            else
                context.Entry(taskStep).State = EntityState.Modified;

            context.SaveChanges();
        }
    }

如果我从带有包含角色的db中获取tasktep(比如save方法中的第一个-comment-行),那么它的工作效果很好。但是这样,我必须手动完成所有tasktep字段的更改。另外,当我将taskstep分配给stepviewmodel时,我使用&#34; include&#34;但它仍然无法正常工作。我该怎么做才能保存stepViewModel角色?

1 个答案:

答案 0 :(得分:0)

context.TaskSteps.Attach(taskStep)适用于您的情况

SaveTaskStep方法:

using (var context = new WorkFlowContext())
{
  TaskStep taskStep = stepViewModel.TaskStep;   
  if (taskStep.ID == 0)
    context.TaskSteps.Add(taskStep);
  else
    context.TaskSteps.Attach(taskStep); //from this point EF start change tracking

//I just copy-pasted this part your code assuming it's correct
  List<int> aRolesIDs = stepViewModel.AllowedRole.Where(ar => ar.Allowed == true).Select(ar => ar.Role.ID).ToList();
  List<Role> roles = context.Roles.Where(r => aRolesIDs.Contains(r.ID)).Include(r => r.AllowedTaskSteps).ToList();

  taskStep.Roles.Clear();
  foreach (Role role in roles)
  {
     if (context.Entry(role).State == EntityState.Detached)
       context.Roles.Attach(role);

       taskStep.Roles.Add(role);
  }

  context.SaveChanges();
} // end of using (var context