2011-10-24 52 views
2

我无法加载给定实体的某些子属性。我已经设法加载其他对象的子实体,但不是这个。添加到挫折,我试图加载的子实体被引用从另一个实体,并从这他们工作正常...RIA无法使用RIA和Silverlight加载子实体

我的代码如下。

ViewWasteApplication页

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元数据类,如下所示:

<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的实体框架4.

+0

除上述之外 - 在GetWasteApplicationsByID中设置断点表明结果包含子元素,但是当调用wasteApplicationLoaded指示数据已加载时,子元素不存在。 –

回答

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 
+0

Gavin, 您的解决方案不会有效地将所有WasteStreams和所有包装项目到客户端,这样相关的项目可以绑定到您的WasteApplication项目上?还是RIA执行某种我不知道的额外魔法?或者我完全错过了这一点? 我会很感激你可以提供的任何反馈,因为我现在有完全相同的问题。 干杯, Scott – Scott

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了解更多。

+0

正如我在原来的文章中提到的,我已经为该对象创建了一个元数据类,但是尽管将这些字段标记为“include”,但它们不会加载 –

1

你需要做以下2个东西,为了这个工作:

一)增加包括在元数据类参数(如DaveB建议)

b)在域服务查询(服务器端)添加include指令--Me.ObjectContext.WasteApplications。包括(“CurrentlyWith”)