locked
A circular reference was detected while serializing an object of type. In 1 to Many relationship RRS feed

  • Question

  • 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.


    Don't miss to 'Vote As Helpful' or 'Mark As Answer' a deserving post.

    Monday, February 10, 2014 10:01 AM

Answers

All replies

  • Works if I remove the navigation property or disable lazy loading by removing the virtual key word. But not real solutions I guess.

    Don't miss to 'Vote As Helpful' or 'Mark As Answer' a deserving post.

    Monday, February 10, 2014 10:17 AM
  • Hello,

    >>Works if I remove the navigation property or disable lazy loading by removing the virtual key word. But not real solutions I guess.

    This will then get master entity without associated entity.

    Is it that you are using JSON to serialize an entity framework?

    If it is, have a try to use Json.NET which supports serialization for EF entities:

    http://james.newtonking.com/archive/2009/07/10/ado-net-entity-framework-support-accidently-added-to-json-net

    EF creates proxy objects which lack the attributes in the POCO objects, and aren't serializable.

    For details, please refer to link below:

    http://johnnycode.com/2012/04/10/serializing-circular-references-with-json-net-and-entity-framework/

    If I misunderstand, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Wednesday, February 19, 2014 9:27 AM
    Tuesday, February 11, 2014 8:37 AM