The problem with the navigation loop on the Entity Framework, although WCF

I have a model like

public class User
{
    [Key]
    public long UserId { get; set; }

    [Required]
    public String Nickname { get; set; }

    public virtual ICollection<Group> Memberships { get; set; }
}

public class Group
{
    [Key]
    public long GroupId { get; set; }

    [Required]
    public String Name { get; set; }

    public virtual ICollection<User> Members { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Group> Groups { get; set; }

    public DataContext()
    {
        Configuration.LazyLoadingEnabled = true;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>()
        .HasMany(u => u.Memberships)
        .WithMany(t => t.Members)
        .Map(x =>
        {
            x.MapLeftKey("UserId");
            x.MapRightKey("GroupId");
            x.ToTable("GroupMembers");
        });
    }
}

Everything goes well when you access objects using a test console application, but I need to have this through the WCF service, here I got this exception:

Failed to call the service. Possible reasons: the service is unavailable or unavailable; client-side configuration does not match proxy; existing proxy is not valid. Refer to the stack trace for more details. You can try to restore by starting a new proxy server, restoring to the default configuration or updating the service

The only way I found this work is to delete the navigator in one of the objects, because the presence of navigators on both sides causes an infinite loop.

?

+5
1

WCF :

  • : ? WCF/ , Include , . . , , , , .
  • . WCF DataContractSerializer. , IgnoreDataMember , DataContract(IsReference = true) , DataMember.
+7

All Articles