0
votes

I have nested object in EF Code First using Silverlight RIA service, I am able to get the data at service side but when I see it on client side child objects are null. Could you please guide me what's wrong.

[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; }
}

and service call is : this.DbContext.Batches.Include("BatchSetItemSets.ItemSet.Custodian").Where(x => x.BatchId == batchId).SingleOrDefault();

1
How are you trying to load your entities client side ? Please, post the codemCasamento
Sorry I didn't get your question properly, I have WCF RIA service which I am calling using following call: this.DbContext.Batches.Include("BatchSetItemSets.ItemSet.Custodian").Where(x => x.BatchId == batchId).SingleOrDefault();Deepak

1 Answers

0
votes

You are close with the attributes, but they need to be changed:

[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; }
}

The AssociationAttribute .ctor is defined as:

AssociationAttribute(string name, string thisKey, string otherKey)

It should be configured as:

  • name: A unique name shared by each end of the relationship
  • thisKey: The name of the Property on this object that represents the key or foreign key
  • otherKey: The name of the Property on the other object that represents the key or foreign key
  • IsForeignKey: A property on AssociationAttribute to indicate if this end of the relationship is the primary key or the foreign key. It defaults to true (meaning this navigation property is a Foreign Key). By setting it to false, you indicate to WCF RIA that this object contains the Primary Key. For all AssociationAttribute usages with a given name, only one is allowed to have IsForeignKey = false. Otherwise, you will get a build error.