实体框架的堆栈溢出异常

时间:2016-06-03 10:53:35

标签: c# entity-framework

我有一个使用Entity Framework的WPF应用程序。在模型中,我有2个类,一个叫做Category,另一个叫Group。

public class Category
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public List<Group> Groups { get; set; }
}

public class Group
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public int CategoryId { get; set; }

    public List<Part> Parts{ get; set; }

    [ForeignKey(nameof(CategoryId))]
    public Category Category { get; set; }
}

我为这两个类创建了Wrappers。

    public partial class CategoryWrapper:ModelWrapper<Category>
{
    public CategoryWrapper(Category model) : base(model)
    {
        InitializeCollectionProperties(model);
    }


    public System.Int32 Id
    {
      get{return GetValue<System.Int32>();}
      set{SetValue(value);}
    }   
    public System.Int32 IdOriginalValue => GetOriginalValue<System.Int32>(nameof(Id));
    public bool IdHasChanged => GetIsChanged(nameof(Id));



    public System.String Name
    {
      get{return GetValue<System.String>();}
      set{SetValue(value);}
    }   
    public System.String NameOriginalValue => GetOriginalValue<System.String>(nameof(Name));
    public bool NameHasChanged => GetIsChanged(nameof(Name));


    public ChangeTrackingCollection<GroupWrapper> Groups { get; set; }
    public override void InitializeCollectionProperties(Category model)
    {

        if (model.Groups ==null)
        {
            return;
        }

        Groups = new ChangeTrackingCollection<GroupWrapper>(
            model.Groups.Select(x => new GroupWrapper(x)));
        RegisterCollection(Groups,model.Groups);

    }

}
public partial class GroupWrapper:ModelWrapper<Group>
{
    public GroupWrapper(Group model) : base(model)
    {
        InitializeCollectionProperties(model);
        InitializeComplexProperties(model);
    }


    public System.Int32 Id
    {
      get{return GetValue<System.Int32>();}
      set{SetValue(value);}
    }   
    public System.Int32 IdOriginalValue => GetOriginalValue<System.Int32>(nameof(Id));
    public bool IdHasChanged => GetIsChanged(nameof(Id));



    public System.String Name
    {
      get{return GetValue<System.String>();}
      set{SetValue(value);}
    }   
    public System.String NameOriginalValue => GetOriginalValue<System.String>(nameof(Name));
    public bool NameHasChanged => GetIsChanged(nameof(Name));



    public System.Int32 CategoryId
    {
      get{return GetValue<System.Int32>();}
      set{SetValue(value);}
    }   
    public System.Int32 CategoryIdOriginalValue => GetOriginalValue<System.Int32>(nameof(CategoryId));
    public bool CategoryIdHasChanged => GetIsChanged(nameof(CategoryId));


    public CategoryWrapper Category { get; private set; }
    public ChangeTrackingCollection<PartWrapper> Parts { get; set; }
    public override void InitializeComplexProperties(Group model)
    {

        if (model.Category ==null)
        {
            return;
        }

        Category = new CategoryWrapper(model.Category);
        RegisterComplex(Category);

    }
    public override void InitializeCollectionProperties(Group model)
    {

        if (model.Parts ==null)
        {
            return;
        }

        Parts = new ChangeTrackingCollection<PartWrapper>(
            model.Parts.Select(x => new PartWrapper(x)));
        RegisterCollection(Parts,model.Parts);

    }

}

然后我有一小段代码调用实体框架。

    public async Task<CategoryWrapper> AsyncCategory(int categoryId)
    {

        return await Task.Run(() => _selectRepository
            .Categories()
            .Where(x => x.Id == categoryId)
            .Select(x => new CategoryWrapper(x))
            .FirstOrDefault());
    }
   // here is Categories
    public IEnumerable<Category> Categories()
    {
        using (var context = new ShipListContext())
        {
            return context.Categories
                .Include(x => x.Groups)
                .ToList();
        }
    }

这段代码是我得到异常的地方。我不确定为什么,因为当我运行我的测试时,我没有得到我的错误。我已经将查询拆分为同一个查询只有3行,我注意到在快速监视窗口中我看到Category.Groups有10个组,每个组都有一个Group.Category,里面有Category.Groups。那么,我该如何阻止这种情况发生?

0 个答案:

没有答案