简体   繁体   English

一对多关系不编辑子表

[英]One-To-Many Relationship not editing Child table

public class Class1
{
    public Guid Class1ID { get; set; }
    public string class1string { get; set; }
    public virtual Class2 Class2 { get; set; }
}

public class Class2
{
    public Guid Class2ID { get; set; }
    public string class2string { get; set; }
}

// POST: Class1/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(Guid id, [Bind("Class1ID,Class2,class1string")] Class1 class1)
    {
        if (id != class1.Class1ID)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(class1);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!Class1Exists(class1.Class1ID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction("Index");
        }
        return View(class1);
    }

Instead of the Edit changing the data that is in the child table, it creates an new row in the table and changes the GUID in the parent table. 而不是使用Edit更改子表中的数据,而是在表中创建新行并更改父表中的GUID。 The Parent table is edited correctly. 父表已正确编辑。

Any help will be greatly appreciated. 任何帮助将不胜感激。

In your Class 1 also add the foreignkey id that should match the primary key property. 在第1类中,还应添加与主键属性匹配的外键ID。 EF will know its related EF将了解其相关信息

public class Class1
{
    public Guid Class1ID { get; set; }
    public string class1string { get; set; }
    public Guid? Class2ID { get; set; }
    [ForeignKey("Class2ID ")]//probably not needed as names match
    public virtual Class2 Class2 { get; set; }
}

public class Class2
{
    public Guid Class2ID { get; set; }
    public string class2string { get; set; }
}

that way in your update class1 you just need to check you pass the correct Class2ID property and not worry about the navigation object property Class2 . 这样,在更新class1您只需要检查您是否传递了正确的Class2ID属性,而不必担心导航对象属性Class2

For saving you need to spesify it was modified 为了节省您需要修饰它,

 public async Task<IActionResult> Edit(Class1 class1)
 {
     ...
     _context.Entry(class1).State = EntityState.Modified;
     await _context.SaveChangesAsync();

I have also been struggling with this problem. 我也一直在努力解决这个问题。

When I make an edit and save it I hit the function public async Task Edit(Guid id,[Bind("Class1ID,Class2,class1string")] Class1 class1) (as you would expect) - 当我进行编辑并保存时,我点击了函数public async Task Edit(Guid id,[Bind(“ Class1ID,Class2,class1string”)] Class1 class1) (如您所愿 )-

All the values are correct except class1.Class2.Class2ID which is an empty guid {00000000-0000-0000-0000-000000000000} class1.Class2.Class2ID外,所有值都是正确的, 该类是空的GUID {00000000-0000-0000-0000-0000-000000000000}

As a result when SaveChangesAsync is called EF creates a new record for Class2 , rather than updating the existing record as intended. 结果,当调用SaveChangesAsync时,EF会为Class2创建一个新记录,而不是按预期更新现有记录。

The is because the binding is failing, it can't find the value for Class2.Class2ID . 是因为绑定失败,所以找不到Class2.Class2ID的值。

This is because the Get is failing to load this value as it is almost certainly not on the page. 这是因为Get无法加载此值,因为几乎可以肯定该值不在页面上。

Add the following line to your view markup (suggest next to input type="hidden" asp-for="Class1ID" ) 将以下行添加到视图标记中(建议在输入type =“ hidden” asp-for =“ Class1ID”旁边

 <input type="hidden" asp-for="Class2.Class2ID" />

This should enable the binding to work. 这应该使绑定起作用。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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