RIA未加载儿童实体RIA& Silverlight的

时间:2011-10-24 13:56:05

标签: silverlight entity-framework ria

我在加载给定实体的某些子属性时遇到困难。我设法在其他对象上加载子实体,但不是这个。为了增加挫折感,我试图加载的子实体是从另一个实体引用的,从中它们可以正常工作......

我的代码如下。

ViewWasteApplication Page

Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

    If NavigationContext.QueryString.ContainsKey("ID") Then

        ' load an existing record - edit mode
        Context.Load(Context.GetWasteApplicationsByIDQuery(Int32.Parse(NavigationContext.QueryString("ID").ToString())),
                     AddressOf wasteApplicationLoaded, Nothing)
    Else
        MessageBox.Show("Application not found")
    End If

End Sub

这会调用GetWasteApplicationsByID - 如下所示:

Public Function GetWasteApplicationsByID(ByVal query As Int32) As IQueryable(Of WasteApplication)
    Dim result = Me.ObjectContext.WasteApplications.Include("CurrentlyWith") _
                                             .Include("Packaging") _
                                             .Include("WasteStream") _
                                             .Where(Function(f) f.ID = query)
    Return result
End Function

正在返回WasteApplication,但3个子实体都没有出现。

我为此WasteApplication创建了一个MetaData类,如下所示:

<MetadataTypeAttribute(GetType(WasteApplications.WasteApplicationsMetaData))> _
Partial Public Class WasteApplications


Friend NotInheritable Class WasteApplicationsMetaData

    'Metadata classes are not meant to be instantiated.
    Private Sub New()
        MyBase.New()
    End Sub

    Public Property ID As Integer
    Public Property RequestedByName As String
    Public Property RequestedByExtension As String
    Public Property CARQRequired As Boolean
    Public Property OriginOfMaterialID As Integer
    Public Property Comments As String
    Public Property MaterialName As String
    Public Property PackagingID As Integer
    Public Property FacilityPath As String
    Public Property ProcessStatus As String
    Public Property DateSubmitted As DateTime
    Public Property RequestedByUsername As String
    Public Property CurrentlyWithID As Integer
    Public Property WasteStreamID As Integer

    <Include()>
    Public Property WasteStream As WasteStreams

    <Include()>
    Public Property Packaging As Packaging


End Class
End Class

有人可以看到这个有什么问题吗?我在另一个页面上加载了相同的两个子对象,这些似乎加载得很好。代码如下:

查看化学品应用程序(可行)

Protected Overrides Sub OnNavigatedTo(ByVal e As   System.Windows.Navigation.NavigationEventArgs)
         Context.Load(Context.GetChemicalApplicationsByIDQuery(Int32.Parse(NavigationContext.QueryString("ID"))),
                     AddressOf wasteApplicationLoaded, Nothing)

End Sub

RIA功能如下:

Public Function GetChemicalApplicationsByID(ByVal query As Int32) As IQueryable(Of ChemicalApplication)
    Return Me.ObjectContext.ChemicalApplications.Include("Chemical") _
                                                .Include("ProcessStatus") _
                                                .Include("PlanningApprover") _
                                                .Include("FacilitiesApprover") _
                                                .Include("MaintenanceApprover") _
                                                .Include("PPCPermit") _
                                                .Include("DischargeConsent") _
                                                .Include("Facility") _
                                                .Include("Packaging") _
                                                .Include("WasteStream") _
                                                .Where(Function(f) f.ID = query)
End Function

有什么建议吗?

注意:我没有发布任何XAML绑定,因为我已通过调试确认源实体不包含子数据,因此绑定不会成为问题。

我正在使用Silverlight 4和Entity Framework 4。

2 个答案:

答案 0 :(得分:3)

您需要为要包含的实体创建元数据类,并使用[Include]属性标记字段。

[MetadataTypeAttribute(typeof(Client.ClientMetadata))]
public partial class Client
{
    internal sealed class ClientMetadata
    {
        private ClientMetadata()
        {
        }

        [Required(ErrorMessage="You must enter a client name")]
        public string Name;

        [Include]
        public EntityCollection<Contact> Contacts;

        [Include]
        public Employee Employee;

        [Include]
        public BillTo BillTo;
    }
    }

有关详情,请参阅RIA Services and relational data

答案 1 :(得分:0)

我设法解决了这个问题,你需要将相应的实体加载到datacontext中以及加载链接的实体。即在我的情况下,我需要添加:

Context.Load(Context.GetWasteStreamsQuery())
Context.Load(Context.GetPackagingQuery())

以便存在链接的子实体。我的完整onNavigate因此看起来像这样:

Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

    If NavigationContext.QueryString.ContainsKey("ID") Then


        Context.Load(Context.GetWasteApplicationsByIDQuery(Int32.Parse(NavigationContext.QueryString("ID").ToString())),
                 AddressOf wasteApplicationLoaded, Nothing)

        Context.Load(Context.GetWasteStreamsQuery())
        Context.Load(Context.GetPackagingQuery())
    Else
        MessageBox.Show("Application not found")
    End If

End Sub