首页 文章

如何使用WCF修复循环引用问题

提问于
浏览
2

我有一组规范化的表格 . 所有者,狗,许可证(这些是主要的)每个所有者可以有多个狗,每个狗可以有多个许可证 . 有一些参考表与每个对象相关联,例如所有者的地址,狗的品种和颜色以及许可证的许可证类型 . 我不相信这些是我的问题 .

这基本上是Code First(使用逆向工程师代码优先工具)所以我没有EDMX . 我有一个循环引用问题,它正在杀了我 . 我读过的所有文章都说只是为了放置IsReference = true属性 . 如果它只是一个亲子关系,这是很好的 . 我有祖父母 - 亲子关系 . 我试过this文章没有运气 . 我看到很多文章,但大多数都是4岁 . 我想要做什么,并在.Net 4(而不是4.5)中工作 . 在这种情况下,我不可能是唯一一个 . 此时我的大脑很难糊涂 .

这是我从数据库中获取对象的代码 . 它就像一个冠军 .

using (DogLicensingContext db = new DogLicensingContext()) {
    Result = (from l in db.Licenses
        .Include(x => x.Dog) // Get the Dog on this License
        .Include(x => x.Dog.Owner)
        .Include(x => x.Dog.Owner.Title)
        .Include(x => x.Dog.Owner.Dogs) // Gets me all of their Dogs
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Licenses))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Licenses.Select(l => l.TagStyle)))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Breed))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Color))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Color1))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.HairLength))
        .Include(x => x.Dog.Owner.Dogs.Select(d => d.Sex))
        .Include(x => x.Dog.Owner.Address)
        .Include(x => x.Dog.Owner.Address.State)
        .Include(x => x.Dog.Owner.Phones)
        .Include(x => x.Dog.Owner.Emails)
        .Include(x => x.Dog.Owner.Phones.Select(p => p.PhoneType))
        .Include(x => x.Dog.Owner.Emails.Select(p => p.EmailType))
    where l.BarCode == BarCode
    select l).FirstOrDefault();
} // using the database

这是业主和狗类的精简视图 .

所有者类别:

[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public int ID { get; set; }

public virtual ICollection<Dog> Dogs { get; private set; }

狗类:

[DataMember]
public Nullable<System.DateTime> UpdatedOn { get; set; }
[DataMember]
public int ID { get; set; }

[DataMember]
public virtual Owner Owner { get; set; }

public virtual ICollection<License> Licenses { get; set; }

它在这样的测试客户端中工作 . 如果我制作ICollection Dogs或ICollection许可证,DataMember就是我获得StackOverflow的时候 .

那么我怎样才能让收藏品遇到WCF?

1 回答

  • 4

    使用 ReferencePreservingDataContractFormat here .

    然后使用以下属性标记您的wcf服务方法(操作 Contract ):

    [OperationContract]
    [ReferencePreservingDataContractFormat]
    List<Dog> GetDogsAndOwners();
    

相关问题