一对多关系不编辑子表

时间:2022-06-03 09:58:41
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. The Parent table is edited correctly.

而不是编辑更改子表中的数据,它在表中创建一个新行并更改父表中的GUID。父表已正确编辑。

Any help will be greatly appreciated.

任何帮助将不胜感激。

2 个解决方案

#1


0  

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-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")

将以下行添加到视图标记中(建议在input type =“hidden”asp-for =“Class1ID”旁边)

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

This should enable the binding to work.

这应该使绑定工作。

I hope this helps.

我希望这有帮助。

#2


1  

In your Class 1 also add the foreignkey id that should match the primary key property. EF will know its related

在类1中还添加应与主键属性匹配的foreignkey id。 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.

在更新类1中,您只需要检查是否传递了正确的Class2ID属性,而不必担心导航对象属性Class2。

For saving you need to spesify it was modified

为了节省你需要spesify它被修改

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

#1


0  

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-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")

将以下行添加到视图标记中(建议在input type =“hidden”asp-for =“Class1ID”旁边)

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

This should enable the binding to work.

这应该使绑定工作。

I hope this helps.

我希望这有帮助。

#2


1  

In your Class 1 also add the foreignkey id that should match the primary key property. EF will know its related

在类1中还添加应与主键属性匹配的foreignkey id。 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.

在更新类1中,您只需要检查是否传递了正确的Class2ID属性,而不必担心导航对象属性Class2。

For saving you need to spesify it was modified

为了节省你需要spesify它被修改

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