【问题标题】:RIA Not Loading Child Entities With RIA & SilverlightRIA 不使用 RIA 和 Silverlight 加载子实体
【发布时间】:2011-10-24 13:56:04
【问题描述】:

我在加载给定实体的某些子属性时遇到了困难。我已经设法在其他对象上加载子实体,但不是这个。更令人沮丧的是,我尝试加载的子实体是从另一个实体引用的,因此它们工作正常......

我的代码如下。

查看WasteApplication 页面

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。

【问题讨论】:

  • 更进一步——在GetWasteApplicationsByID 中设置断点表明result 包含子元素,但是当调用wasteApplicationLoaded 表示数据已加载时,子元素不存在。

标签: silverlight entity-framework ria


【解决方案1】:

您需要为要包含的实体创建元数据类,并使用[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 了解更多信息。

【讨论】:

  • 正如我原来的帖子中提到的,我已经为对象创建了一个元数据类,但是尽管将这些字段标记为include,但它们不会加载
【解决方案2】:

您需要执行以下 2 件事才能使其正常工作:

a) 在元数据类中添加包含参数(如 DaveB 建议的那样)

b) 在域服务查询(服务器端)上添加包含指令 - Me.ObjectContext.WasteApplications.Include("CurrentlyWith")

【讨论】:

    【解决方案3】:

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

    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
    

    【讨论】:

    • Gavin,您的解决方案不会有效地将所有的 WasteStreams 和所有 Packaging 项目拉到客户端,以便相关的项目可以绑定到您的 WasteApplication 项目吗?或者 RIA 是否在执行某种我不知道的额外魔法?还是我完全错过了重点?感谢您提供任何反馈,因为我目前遇到了完全相同的问题。干杯,斯科特
    猜你喜欢
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多