Entity Framework Navigation Properties looping issue though WCF

非 Y 不嫁゛ 提交于 2019-12-07 05:08:08

问题


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");
        });
    }
}

All goes fine when accessing the entities using a test console application, but I need to have this through a WCF service, here I got this exception:

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service

The only way I found to have this working is, removing the navigator in one of the entities, because having the navigators in both sides causes a infinite looping.

Is there a way to have this working without removing the navigators?


回答1:


There are two issues if you try to use WCF:

  • First issue: Do you want to return related entities as well? Always turn off lazy loading when working with WCF / serialization and make sure that you manually use Include for relations you really want to return. Otherwise lazy loading will load all relation in the object graph. Moreover depending on the way how your service handles context's life cycle, the lazy loading can happen when the context is already disposed.
  • You must tell serializer about the circular reference or remove the circular reference. WCF by default uses DataContractSerializer. To remove the circular reference you can mark one of those navigation properties with IgnoreDataMember attribute or you can simply tell serializer about the circular reference by marking all entities with DataContract(IsReference = true) attribute and all member properties which should be serialized with DataMember attribute.


来源:https://stackoverflow.com/questions/11715120/entity-framework-navigation-properties-looping-issue-though-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!