【发布时间】: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