如何在EF Core中使用新值更新导航属性? [英] How to update naviation property with new values in EF Core?

查看:585
本文介绍了如何在EF Core中使用新值更新导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

S ...我有一个用于Product的简单模型:

Soo... I have this simple model for Product:

public class Product {
    public int Id { get; set; }

    // Other things

    public ICollection<ProductAttribute> ProductAttributes { get; set; }
}

ProductAttributes,其中ProductIdName作为多字段键.

And ProductAttributes with ProductId and Name as multifield keys.

public class ProductAttribute {
    public int? ProductId { get; set; } // Key
    public ProductAttributeName? Name { get; set; } // Key
    public string Value { get; set; }
}

在我的WebAPI项目中,我有这种方法:

And in my WebAPI project I have this method:

public async Task<IActionResult> Patch(Product product) {
    var productExist = await _context.Products.AnyAsync(a => a.Id == product.Id);
    if (!productExist) return NotFound(product.Id);
    _context.Products.Update(product);
    await _context.SaveChangesAsync();
    return Ok();
}

我假设如果某人在productAttributes中使用不同的数据发送我的JSON,我应该将值切换为完全覆盖的新值 或删除旧值.就像在这个例子中一样

And I assume that if someone sends my JSON with different data in productAttributes I should switch values to the new ones completely overriding or deleting old values. Like in this example

OldProduct
* Name
* ProductAttributes:
    * Value 1
    * Value 2

NewProduct
* Name
* ProductAttributes:
    * Value 2
    * Value 3

因此,应删除Value1并添加Value3.但是,相反,我在SaveChangesAsync()上遇到了异常:

So Value1 should be deleted and Value3 should be added. But instead, I'm getting an exception on SaveChangesAsync():

System.ArgumentException: 'An item with the same key has already been added. Key: System.Object[]'

我猜可能是因为它试图添加Value2,但是它已经存在.

I guess it's probably because it's trying to add Value2, but it already exists.

如何正确更新naviagation属性?

How should I properly update the naviagation property?

@更新

由于@ cjens19,我只是更改了更新方法并使用了Automapper:

Thanks to @cjens19 I just change update method and used Automapper:

public async Task<IActionResult> Patch(Product product) {
    var productFromDb = await _context.Products.Include(p => p.ProductAttributes).SingleOrDefaultAsync(product1 => product1.Id == product.Id);
    if (productFromDb == null) return NotFound(product.Id);
    Mapper.Map(product, productFromDb);
    await _context.SaveChangesAsync();
    return Ok();
}

推荐答案

////正在检查模型是否存在代码?

What are you doing in the // ... Checking if model exist code?

您应该使用以下类似的方法从上下文中检索现有的Product实体:

You should be retrieving the existing Product entity out of the context with something like:

var entity = _context.Products.SingleOrDefault(p => p.Id == product.Id);

然后,如果实体不为null,则可以使用随Product参数传递的值来设置其属性.

Then, if entity is not null, you can set his properties with the values passed in with the Product param.

最后,您将调用更新和保存.这不应该给您任何ef核心错误.

Lastly, you will call Update and Save. This shouldn't give you any ef core errors.

这篇关于如何在EF Core中使用新值更新导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆