My code first model has a one-to-many relationship. The database is successfully generated using EF6 migrations. I've created a drop-down list but cannot update the database with the user's selection.
The LiquidFormulas table contains a foreign key GravureCylinderID (nullable) which points to the ID field in the table GravureCylinders. Here are my two model classes:
public partial class LiquidFormula
{
    public int ID { get; set; }
    ...
    public int? GravureCylinderID { get; set; }
    [ForeignKey("GravureCylinderID")]
    public virtual GravureCylinders GravureCylinder { get; set;}     
}
public class GravureCylinders
{
    public int ID { get; set; }
    ...
    public ICollection<LiquidFormula> LiquidFormulas { get; set; }
}
My LiquidFormulaViewModel references the LiquidFormula model and holds a lookup list of GravureCylinders:
public class LiquidFormulaViewModel
{
    public LiquidFormulaViewModel()
    {
        LiquidFormula = new LiquidFormula();
    }
    public LiquidFormula LiquidFormula { get; set; }
    ...
    public IEnumerable<SelectListItem> GravureCylinderList { get; set; }
In the Formulas controller I populate the lookup list:
public ActionResult Edit(int? id)
{
    LiquidFormula liquidFormula = _context.FormulasDbSet.Find(id);
    ...
    var formulaVM = new LiquidFormulaViewModel();
    formulaVM.LiquidFormula = liquidFormula;
    formulaVM.GravureCylinderList = GetListOfGravureCylinderValues();
Then in the view I display the lookup list and bind it to the foreign key:
<select asp-for="LiquidFormula.GravureCylinderID" asp-items="@Model.GravureCylinderList" class="form-control">
    <option>Select Volume in BCM</option>
</select>
I've also added this hidden field to the view:
 <input asp-for="@Model.LiquidFormula.GravureCylinder.ID" type="hidden" />
When the view form is posted I'm struggling to get the data to save properly using _context.SaveChanges(); Before SaveChanges I see the new LiquidFormula.GravureCylinderID in the ViewModel but after SaveChanges the database is NOT updated with the new ID.
EDIT: I fixed the referential integrity constraint error (below) by removing this hidden field from the view:
<input asp-for="@Model.LiquidFormula.GravureCylinder.ID" type="hidden" />
But still the database is not updating with the new LiquidFormula.GravureCylinderID.
Before _context.SaveChanges(); I get a referential constraint error on this line which is required for other work I'm doing:
_context.FormulasDbSet.Attach(modifiedFormulaVM.LiquidFormula);
Here is the error message:
A referential integrity constraint violation occurred: The property value(s) of 'GravureCylinders.ID' on one end of a relationship do not match the property value(s) of 'LiquidFormula.GravureCylinderID' on the other end.
By examining the two IDs I see that LiquidFormula.GravureCylinderID has been updated and GravureCylinders.ID has not. How can I properly update both IDs particularly GravureCylinders.ID?
