2

我有一组标准化的表格。所有者,狗,许可证(这些是主要的)每个所有者都可以拥有多只狗,每只狗都可以拥有多个许可证。有一些参考表与每个对象相关联,例如所有者的地址、狗的品种和颜色以及许可证的 LicenseType。我不相信这些是我的问题。

这本质上是 Code First(使用逆向工程师 Code First 工具),所以我没有 EDMX。我有一个循环引用问题,这让我很生气。我读过的所有文章都说只是为了把 IsReference = true 属性放在上面。如果只是亲子关系,那就太好了。我有祖父母 - 父母 - 孩子的关系。我试过这篇文章没有运气。我看到了很多文章,但大多数都是 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

这是 Owner 和 Dog 类的精简视图。

业主等级:

[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 Licenses 成为 DataMember,那么当我获得 StackOverflow 时。

那么我怎样才能让集合遇到 WCF?

4

1 回答 1

4

使用ReferencePreservingDataContractFormat 这里

然后用属性标记你的 wcf 服务方法(操作契约):

[OperationContract]
[ReferencePreservingDataContractFormat]
List<Dog> GetDogsAndOwners();
于 2012-11-19T15:41:46.140 回答