实体框架导航属性循环问题虽然WCF [英] Entity Framework Navigation Properties looping issue though WCF

查看:148
本文介绍了实体框架导航属性循环问题虽然WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

[必需]
public String昵称{get;组; }

public virtual ICollection< Group>会员{get;组;
}

public class Group
{
[Key]
public long GroupId {get;组;

[必需]
public String Name {get;组; }

public virtual ICollection< User>会员{get;组; }
}

public class DataContext:DbContext
{
public DbSet< User>用户{get;组; }
public DbSet< Group>组{get;组; }

public DataContext()
{
Configuration.LazyLoadingEnabled = true;


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

使用测试控制台访问实体时,一切正常应用程序,但我需要通过一个WCF服务,这里我有这个例外:


无法调用该服务。可能原因:服务离线或无法访问;客户端配置与
代理不匹配;现有代理无效。请参阅堆栈跟踪
更多的细节。您可以尝试通过启动新的代理恢复,恢复
到默认配置,或刷新服务


唯一的方法我发现这个工作是在其中一个实体中删除导航器,因为双方的导航器都会导致无限循环。



有没有办法这个工作没有删除导航器?

解决方案

如果您尝试使用WCF,有两个问题:




  • 第一个问题:您还想返回相关实体吗?使用WCF /序列化时,始终关闭延迟加载,并确保您手动使用包含您真正希望返回的关系。否则延迟加载会加载对象图中的所有关系。此外,根据您的服务处理上下文的生命周期的方式,当上下文已被处理时,可能会发生懒惰加载。

  • 您必须告诉serializer关于循环引用或删除循环引用。默认情况下,WCF使用 DataContractSerializer 。要删除循环引用,您可以使用 IgnoreDataMember 属性来标记其中一个导航属性,也可以通过使用标记所有实体来简单地告诉serializer关于循环引用属性和所有成员属性应该使用 DataMember 属性序列化。


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?

解决方案

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.

这篇关于实体框架导航属性循环问题虽然WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆