我有4个相关实体,如下所示
public class VariantOption
{
public virtual Variant Variant { get; set; }
}
public class ProductVariant
{
public virtual Product Product { get; set; }
public virtual Variant Variant { get; set; }
public virtual VariantOption DefaultVariantOption { get; set; }
public virtual List<VariantOption> Selected_Options { get; set; }
}
public class Product
{
public virtual List<ProductVariant> ProductVariants { get; set; }
}
public class Variant
{
public virtual List<VariantOption> Var_Options { get; set; }
public virtual VariantOption DefaultVariantOption { get; set; }
}
在更新方法中,我执行类似
的操作 public bool UpdateProductVariant(ProductVariant productVariant){
var dbProductVariant = context.ProductVariant.Find(productVariant.Id);
dbProductVariant.DefaultVariantOption = context.VariantOptions.Find(productVariant.DefaultVariantOption.Id);
dbProductVariant.VariantOptions.Clear();
dbProductVariant.VariantOptions =
productVariant.VariantOptions.Select(x => context.VariantOptions.Find(x.Id)).ToList();
context.SaveChanges();
}
问题是这个