我在EF Code First中使用Silverlight RIA服务嵌套对象,我能够在服务端获取数据,但是当我在客户端看到它时,子对象为空。你能指导我出错了。
[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();
答案 0 :(得分: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; }
}
AssociationAttribute
.ctor定义为:
AssociationAttribute(string name, string thisKey, string otherKey)
应配置为:
name
:关系的每一端共享的唯一名称thisKey
:此对象上表示键或外键的属性名称otherKey
:另一个对象上表示键或外键的属性名称IsForeignKey
:AssociationAttribute
上的一个属性,用于指示关系的这一端是主键还是外键。它默认为true
(意味着此导航属性是外键)。通过将其设置为false
,您向WCF RIA指示此对象包含主键。对于具有给定AssociationAttribute
的所有name
次使用,只允许一个IsForeignKey = false
。否则,您将收到构建错误。