I have two Entities

public class ProductOption
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int OptionId { get; set; }

    [Required]
    [MaxLength(40)]
    public String OptionName { get; set; }
}

public class ProductOptionValue
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ValueId { get; set; }
    public int OptionId { get; set; }

    [Required]
    [MaxLength(32)]
    public String ValueName { get; set; }
}

ABOVE Entities

Optionid --Primary key(ProductOption table)

Optionid --foreignkey (ProductOptionValue table)

I have 2 model classes

public class ProductOptionRequest
{
    public string Name { set; get; }
    public List<ProductValuesRequest> productValues { get; set; }
}

public class ProductValuesRequest
{
    public int OptionId { get; set; }
    public string ValueName { get; set;}
}

now I want to update data into those tables. I wrote below code

public async Task<ReturnBoolean> UpdateProductOption(ProductOptionRequest request)
{
        ReturnBoolean returnBoolean = new ReturnBoolean();
        ProductOption option = await productOptionRepository.GetByIdAsync(request.Id);
        option.OptionName = request.Name;
        await productOptionRepository.UpdateAsync(option, true);
        foreach (ProductValuesRequest valueRequest in request.productValues)
        {
            ProductOptionValue res = new ProductOptionValue();
            res.ProductOption = option;
            res.ValueName = valueRequest.ValueName;
            await productOptionValueRepository.UpdateAsync(res, true);
        }
        returnBoolean.BooleanValue = true;
        return returnBoolean;
    }

the problem is valueid (its identity column in ProductOptionValue table). So kindly let me know the solution for it. How to pass valueid and update those queries.

0 Answers