【问题标题】:Entity framework foreign table not accessible in xaml code behind在后面的 xaml 代码中无法访问实体框架外部表
【发布时间】:2010-11-11 21:14:08
【问题描述】:

我正在尝试使用 RIA 服务将 IQueryable 从我的 DomainService 类中提取到我的 XAML 代码中。

DomainService 从 BLL 中提取,从 DAL 中提取,从 EF 中获取。

我似乎无法访问 XAML 中的外部表,但我可以在 DomainService 方法中正常访问它。

DomainService 方法如下所示...

    public IQueryable<MenuHeader> GetMenuHeaders()
    {
        BusinessLogic.Employee blEmployee = new BusinessLogic.Employee();

        int employeeId = blEmployee.GetEmployeeIdFromUserName(HttpContext.Current.User.Identity.Name);
        var menuHeaders = blEmployee.GetEmployeeMenuHeaders(employeeId);
        // This works here!
        var menuHeaderItems = from mh in menuHeaders
                              select mh.MenuHeaderItems;
        return menuHeaders;
    }

在后面的 XAML 代码中,我在这里调用了这个方法:

...

EmployeeContext employeeContext = new EmployeeContext();

EntitySet<MenuHeader> menuHeaders = employeeContext.MenuHeaders;            
employeeContext.Load(employeeContext.GetMenuHeadersQuery()).Completed += (s, e) =>
    {
        // This does NOT work here!
        var menuHeaderItems = from mh in menuHeaders
                              select mh.MenuHeaderItems; // <-- Not found
    };

...

如何将这个表添加到我的 XAML 代码,以便我可以对其进行数据绑定?

【问题讨论】:

    标签: silverlight entity-framework ria domainservices


    【解决方案1】:

    您的发送和接收代码看起来完全正确。如果服务器端正确返回了您的测试代码中的实体,则说明出现了严重错误。我已经尝试了几种变体,并且无法让类似的代码失败永远

    顺便说一句,您有 3 个选项来访问从 RIA 服务返回的集合:

    1.在上下文中使用特定的集合(就像你一样)

    EntitySet<MenuHeader> menuHeaders = employeeContext.MenuHeaders;
    ... // On Completed event
    var menuHeaderItems = from mh in menuHeaders
                              select mh.MenuHeaderItems;
    

    2。使用加载操作 Entities 属性

    EmployeeContext employeeContext = new EmployeeContext();
    var LoadOp = employeeContext.Load(employeeContext.GetMenuHeadersQuery());
    // Bind to LoadOp.Entities
    

    3.使用 Load() 回调参数

    EmployeeContext employeeContext = new EmployeeContext();
    var LoadOp = employeeContext.Load(employeeContext.GetMenuHeadersQuery(),
          (cb) => 
          {
                // do something with cb.Entities here
          }, 
          false);
    

    我更喜欢版本 2 用于延迟绑定,而版本 3 用于任何自定义响应(只是为了减少代码行数)。

    【讨论】:

      【解决方案2】:

      这里的问题是我的实体模型不是我的 ASP.Net/RIA 应用程序的一部分。仅在中引用。

      由于它是一个引用,DomainService 不会自动生成元数据。我必须手动创建元数据,以便 Silverlight 知道会发生什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-24
        • 1970-01-01
        相关资源
        最近更新 更多