Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 集合被修改时出错_C#_Asp.net Mvc_Ef Code First_One To Many - Fatal编程技术网

C# 集合被修改时出错

C# 集合被修改时出错,c#,asp.net-mvc,ef-code-first,one-to-many,C#,Asp.net Mvc,Ef Code First,One To Many,我在删除对象时遇到问题,因为它与其他对象有关系。 我正在使用MVC4和代码优先数据库方法 这是我的模型课: public class Product { public Product() { } public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public bool Istaxable { get; set;

我在删除对象时遇到问题,因为它与其他对象有关系。 我正在使用MVC4和代码优先数据库方法

这是我的模型课:

 public class Product
{
    public Product() { }

    public int Id { get; set; } 
    public string Name { get; set; }
    public decimal Price { get; set; }
    public bool Istaxable { get; set; }
    public string DefaultImage { get; set; }
    public IList<Feature> Features { get; set; }
    public IList<Descriptor> Descriptors { get; set; }
    public IList<Category> Categories { get; set; }
    public IList<Image> Images { get; set; }

    public Product(string name, decimal price, bool istaxable, string defaultImageFile)
    {
        Name = name;
        Price = price;
        Istaxable = istaxable;
        DefaultImage = defaultImageFile;
        Categories = new List<Category>();
        Features = new List<Feature>();
        Descriptors = new List<Descriptor>();
        Images = new List<Image>();
    }
}

 public class Image
{

    public Image() { }

    public Image(string thumb, string full) : this(thumb, full, false) { }

    public Image(string thumb, string full, bool isDefault)
    {
        Thumb = thumb;
        IsDefault = isDefault;
        Full = full;
    }

    public int Id { get; set; }
    public string Thumb { get; set; }
    public string Full { get; set; }
    public bool IsDefault { get; set; }
    public string Description { get; set; }

}
Uow是作为工作单元构造的my Repository类

当我尝试运行应用程序时,它会删除关系对象,但不会删除产品对象,因为它表示集合已被修改

我应该如何重构此代码以使其正常工作


提前感谢。

您在使用时不能更改基础集合,我建议您使用基本的
for
语句

从以上MSDN链接:

foreach语句用于遍历集合以获取所需的信息,但不能用于从源集合中添加或删除项以避免不可预知的副作用。如果需要从源集合中添加或删除项,请使用for循环


我会尝试删除第一个
Uow.Commit()
,最后只执行一个。
// DELETE /api/product/5
    public HttpResponseMessage Delete(int id)
    {

        var prd = Uow.Products.GetProductByIdIncludeAll(id);

        var images = prd.Images;
        if ( images.Count > 0 )
        {
            foreach(Image i in images)
            {
                Uow.Images.Delete(i);
            }
        }

        var descriptors = prd.Descriptors;
        if (descriptors.Count > 0)
        {
            foreach (Descriptor d in descriptors)
            {
                Uow.Descriptors.Delete(d);
            }
        }

        var features = prd.Features;
        if (features.Count > 0)
        {
            foreach (Feature f in features)
            {
                Uow.Features.Delete(f);
            }
        }

        Uow.Commit();

        Uow.Products.Delete(id);
        Uow.Commit();

        return new HttpResponseMessage(HttpStatusCode.NoContent);
    }