0

I have to 2 simple classes as following:

PermissionGroup:

public class PermissionGroup
    {
        public int PermissionGroupId { get; set; }
        public string PermissionGroupName { get; set; }
        public virtual ICollection<Permission> Permissons { get; set; }
    }

Permission:

public class Permission
{
    public int PermissionId { get; set; }
    public string PermissionName { get; set; }
    public int PermissionGroupId { get; set; }
    public PermissionGroup PermissionGroup { get; set; }
}

and the DataContext class:

public class DataContext : DbContext
{
    public DataContext() : base("name=con"){ }
    public DbSet<PermissionGroup> PermissionGroups { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Permission>().HasRequired(p => p.PermissionGroup).WithMany(f => f.Permissons).HasForeignKey(fk=>fk.PermissionGroupId);
    }
}

I made the configuration exactly like from this article. But still I get the circular reference error which only got when dealing with self referencing object and not in this kind of one to many relationships.

Any suggestion would be appreciated.

  • If I disable lazy loading by removing `virtual` keyword then it works but that is not a solution I think. – user3283426 Feb 10 '14 at 09:40
  • It also works if I remove the Navigation property but again not a real solution I guess. – user3283426 Feb 10 '14 at 10:15
  • Where do you get this error? – boindiil Feb 11 '14 at 09:24
  • In case you are trying to serialize the data into JSON (for a JSONResult in an MVC project) format use [ScriptIgnore] attribute on top of the PermissionGroup property of Permission(child) class, which will stop the recursive loop. In your case it's like this: [ScriptIgnore] public PermissionGroup PermissionGroup { get; set; } – Hasteq Aug 17 '16 at 18:01

0 Answers0