0

我使用 Silverlight RIA 服务在 EF Code First 中嵌套了对象,我能够在服务端获取数据,但是当我在客户端看到它时,子对象为空。你能指导我有什么问题吗?

[HasSelfValidation]
public class Batch
{

    public int BatchId { get; set; }
    public string BatchName { get; set; }
    [Include]
    [Composition]
    [Association("FK_Batch_BathSetItemSet", "BatchId", "BatchSetIdItemSetId")]
    public virtual ICollection<BatchSetItemSet> BatchSetItemSets { get; set; }
 }
public class BatchSetItemSet
{

    public int BatchSetIdItemSetId { get; set; }
    public int BatchId { get; set; }
    public Nullable<int> ItemSetId { get; set; }
    public string CreatedBy { get; set; }
    public  Batch Batch { get; set; }
     [Include]
     [Composition]
     [Association("FK_BathSetItemSet_ItemSet", "BatchSetIdItemSetId", "ItemSetId")]
    public  ItemSet ItemSet { get; set; }
}
public class ItemSet
{

    public int ItemSetId { get; set; }
    public int CustodianId { get; set; }
    public string ItemSetName { get; set; }
    public virtual ICollection<BatchSetItemSet> BatchSetItemSets { get; set; }
    [Include]
    [Composition]
    [Association("FK_ItemSet_Custodian", "ItemSetId", "CustodianId")]
    public virtual Custodian Custodian { get; set; }
}

服务电话是: this.DbContext.Batches.Include("BatchSetItemSets.ItemSet.Custodian").Where(x => x.BatchId == batchId).SingleOrDefault();

4

1 回答 1

0

您与属性很接近,但需要更改它们:

[HasSelfValidation]
public class Batch
{
    public int BatchId { get; set; }
    public string BatchName { get; set; }

    [Include]
    [Composition]
    [Association("FK_Batch_BathSetItemSet", "BatchId", "BatchId", IsForeignKey = false)]
    public virtual ICollection<BatchSetItemSet> BatchSetItemSets { get; set; }
 }

public class BatchSetItemSet
{
    public int BatchSetIdItemSetId { get; set; }
    public int BatchId { get; set; }
    public Nullable<int> ItemSetId { get; set; }
    public string CreatedBy { get; set; }

    [Include]
    [Association("FK_Batch_BathSetItemSet", "BatchId", "BatchId")]
    public Batch Batch { get; set; }

    [Include]
    [Association("FK_BathSetItemSet_ItemSet", "ItemSetId", "ItemSetId")]
    public ItemSet ItemSet { get; set; }
}

public class ItemSet
{
    public int ItemSetId { get; set; }
    public int CustodianId { get; set; }
    public string ItemSetName { get; set; }

    [Include]
    [Composition]
    [Association("FK_BathSetItemSet_ItemSet", "ItemSetId", "ItemSetId", IsForeignKey = false)]
    public virtual ICollection<BatchSetItemSet> BatchSetItemSets { get; set; }

    [Include]
    [Association("FK_ItemSet_Custodian", "CustodianId", "CustodianId")]
    public virtual Custodian Custodian { get; set; }
}

.ctorAssociationAttribute定义为:

AssociationAttribute(string name, string thisKey, string otherKey)

它应该配置为:

  • name:关系的每一端共享的唯一名称
  • thisKey: 此对象上的属性名称,表示键或外键
  • otherKey:表示键或外键的其他对象上的属性名称
  • IsForeignKey: on 属性AssociationAttribute,表示关系的这一端是主键还是外键。它默认为true(意味着此导航属性是外键)。通过将其设置为false,您向 WCF RIA 表明该对象包含主键。对于一个给定的所有AssociationAttribute用法name,只允许有一个IsForeignKey = false。否则,您将收到构建错误。
于 2012-11-27T00:03:25.300 回答