【问题标题】:Code First EF6 Self Referencing Many-To-Many Throws Stack Overflow ErrorCode First EF6 自引用多对多引发堆栈溢出错误
【发布时间】:2016-06-04 22:45:45
【问题描述】:

尝试使用以下设置查找用户时出现堆栈溢出错误。我已尝试关闭延迟加载和代理创建,但我仍然收到错误消息。

public class Authority
{
    public int Id { get; set; }
    public string Domain { get; set; }
    public string Name { get; set; }
    public AuthorityTypeEnum Type { get; set; }
    public virtual List<Authority> Groups { get; set; }
}

关于模型创建:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Entity<Authority>()
                    .HasMany(a => a.Groups)
                    .WithMany().
                    Map(m =>
                        {
                            m.MapLeftKey("UserId");
                            m.MapRightKey("GroupId");
                            m.ToTable("UsersGroups");
                        }
                    );

        base.OnModelCreating(modelBuilder);
    }

上下文构造函数:

public Context()
{
    this.Configuration.LazyLoadingEnabled = true;
    this.Configuration.ProxyCreationEnabled = true;
}

这是引发堆栈溢出的代码:

var byUserAndDomain = db.Authorities
                        .FirstOrDefault(a => a.Type == AuthorityTypeEnum.User && a.Domain == MvcApplication.Domain && a.Name == MvcApplication.UserName);

【问题讨论】:

  • 我刚刚将 MvcApplication.Domain 和 MvcApplication.UserName 替换为常量,但仍然遇到相同的堆栈溢出错误。

标签: c# entity-framework many-to-many stack-overflow self-reference


【解决方案1】:

我发现了问题。虽然错误是在 Linq 表达式的行上抛出的,但递归是在外部范围内发生的。我猜实体框架对我正在使用的对象的递归容忍度最低。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2011-05-06
    • 1970-01-01
    • 2011-08-11
    相关资源
    最近更新 更多