[英]Code First EF6 Self Referencing Many-To-Many Throws Stack Overflow Error
尝试使用以下设置查找用户时,出现堆栈溢出错误。 我尝试同时关闭“延迟加载”和“代理创建”,但是仍然出现错误。
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);
我发现了问题。 尽管在Linq表达式上抛出了错误,但是递归在外部范围中发生。 我猜想实体框架对我正在使用的对象的递归具有最低的容忍度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.