简体   繁体   中英

How to update data in a related table in EF?

There are two such models:

public class Form
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public Guid FormId { get; set; }

   public string Title { get; set; }

   public string Description { get; set; }

   public List<BlockWorkingForm> BlocksWorkingForm { get; set; }
}


public class BlockWorkingForm
{
   [Key]
   [Column(Order = 1)]
   public string Header { get; set; }

   [Key]
   [Column(Order = 2)]
   public Guid FormId { get; set; }

   public Form Form { get; set; }

   public string Field { get; set; }

   public bool MandatoryQuestion { get; set; }

   public override bool Equals(object obj)
   {
       if (obj == null)
       {
           return false;
       }
       if (!(obj is BlockWorkingForm m))
       {
           return false;
       }

       return m.Header == this.Header
               && m.Field == this.Field
               && m.Type == this.Type
               && m.MandatoryQuestion == this.MandatoryQuestion;
   }
}

And there is such a method for updating the model.

public void UpdateForm(Form form)
{
   EditorFormContext context = new EditorFormContext();

   var formDb = this.context.Forms.Include(x => x.BlocksWorkingForm).Single(x => x.FormId == form.FormId);
   this.context.Entry(formDb).CurrentValues.SetValues(form);

   foreach (var itemForm in form.BlocksWorkingForm)
   {
       if (itemForm.FormId == Guid.Empty)
       {
           itemForm.FormId = formDb.FormId;
           this.context.BlocksWorkingForm.Add(itemForm);
       }
       foreach (var itemFormDb in formDb.BlocksWorkingForm)
       {
           if (itemForm.Header != itemFormDb.Header)
           {
               continue;
           }
           if (!itemForm.Equals(itemFormDb))
           {     
               this.context.Entry(itemFormDb)
                           .CurrentValues.SetValues(itemForm);
           }
       }
   }
   this.context.SaveChanges()
}

Now it only allows updating the Title and Description fields in the Database in the Form, as well as adding new blocks (BlockWorkingForm) for the form. But it is still necessary to implement the removal of these blocks.

To remove blocks, I need to compare what is in the database and what came in the Update method, but how can this be done?

This this.context.Entry(formDb).CurrentValues.SetValues(form); is where your properties (Title and Description) are set in the DB object. But The list of BlocksWorkingForm is not set (or not set properly).

If you add the BlocksWorkingForms in the form yourself, the insert should work properly.

This should work.

public void UpdateForm(Form form)
{
   EditorFormContext context = new EditorFormContext();

   var formDb = this.context.Forms.Include(x => x.BlocksWorkingForm).Single(x => x.FormId == form.FormId);
   this.context.Entry(formDb).CurrentValues.SetValues(form);

   formDb.BlocksWorkingForm = form.BlocksWorkingForm;

   this.context.SaveChanges()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM