实体框架核心不会更新ICollection

ShaneKm

尝试对具有嵌套列表的实体执行更新。无论我做什么,我都会不断收到此错误。我已经尝试过:在实体框架中更新嵌套对象,并且:实体框架更新嵌套对象

数据库操作预期会影响1行,但实际上会影响0行。自加载实体以来,数据可能已被修改或删除。有关了解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=527962

“ TypeName”:“ Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException”

插入新行就可以了。但不更新。

public class BuyGroup
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(150)")]
    public string Name { get; set; }

    public virtual ICollection<Item> Items { get; set; } = new List<Item>();
}

public class Item
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    [Column(TypeName = "nvarchar(100)")]
    public string Name { get; set; }

    public BuyGroup BuyGroup { get; set; }
}

仓库:

public async Task<Guid> Save(Entities.BuyGroup model)
{
    using (var dc = _dataContext())
    {
        // this is ok, i get existing item with Items collection populated
        var existing = await Get(model.Id); 

        if (existing != null)
        {
            existing.Name = model.Name;
            ... // overwrite properties

            existing.Items = model.Items; // overwrite Items colletion

            dc.BuyGroups.Update(existing).State = EntityState.Modified;
        } 
        else 
        {
            await dc.BuyGroups.AddAsync(model);
        }

        // blows up here when existing != null
        await dc.SaveChangesAsync();
    }
}

编辑:

添加Get()方法

{
    using (var dc = _dataContext())
    {
        return await dc.BuyGroups.FirstOrDefaultAsync(x => x.Id == id);
    }
}

编辑2:

使用相同的上下文仍然无法解决我的问题:

using (var dc = _dataContext())
{
    var existing = await dc.BuyGroups.FirstOrDefaultAsync(x => x.Id == id); // same context
    if (existing != null)
    {

        existing.Items.Add(new Item{ .....}):
        dc.ByGroups.Entry(existing).State = EntityState.Modified;


    } else {
        await dc.BuyGroups.AddAsync(model);
    }
    await dc.SaveChangesAsync();
}
约翰尼5

我的印象是,由于您正在调用function _dataContext(),而不是仅调用_dataContext的受保护实例,因此可能是在get和save方法中创建了一个新上下文。

由于get和save方法使用的是单独的,DbContexts因此当您existing.Items = model.Items;使用附加到单独的上下文的项目时。

有很多方法可以解决此问题,但就我个人而言,我将创建受保护的方法来接受dbContext,因此您不必担心将实体附加到上下文。

protected BuyGroup GetImplementation(MyDbContext context, int id)
{
    return await context.BuyGroups.FirstOrDefaultAsync(x => x.Id == id);
}

然后,您可以在save方法中调用:

var existing = await this.GetImplementation(dc, model.Id);

编辑编辑

您正在将新项目设置为修改而不是添加

existing.Items.Add(new Item{ .....}):
//You shouldn't do this for added items
//dc.ByGroups.Entry(existing).State = EntityState.Modified;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章