首页 > 解决方案 > 无法更新多个表中的 Isactive 标志状态

问题描述

我有模型课

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public bool IsActive { get; set; }
    public virtual ICollection<ProductSKU> ProductSKUs { get; set; 
 }

public class ProductSKUResponse
{
    public int Skuid { get; set; }
    public String Sku { get; set; }
    public decimal MRP { get; set; }
    public bool IsActive { get; set; }
}

我想更新isactive多个表中的状态,但我没有找到解决方案,所以恳请您让我知道逻辑。

public async Task<ReturnBoolean> DeleteProductById(int Id, bool status)
{
    ReturnBoolean response = new ReturnBoolean();
    Contracts.Entities.Product product = await _productRepository.GetByIdAsync(Id);

    if (product == null)
    {
        throw new ArgumentException("Unable to delete selected product, product does not exist");
    }

    product.IsActive = status;
    product.UpdatedOn = DateTime.Now;
    product.ProductSKUs = await productSkuRepository.GetProductSkus(Id);

    // from this onwards I didn't understand how to assign status value to in product.ProductSKUs.isactive.

    await _productRepository.UpdateAsync(product, true);
    response.BooleanValue = true;
    return response;
}

标签: c#.netentity-frameworklinqasp.net-web-api

解决方案


尝试简单的 foreach

foreach(ProductSKU productSKU in product.ProductSKUs.ToList())
{
   productSKU.IsActive = status;
}

但是也要看这个方法是怎么实现的_productRepository.UpdateAsync(product, true)


推荐阅读