【问题标题】:Silverlight 4 with RIA - Binding joined table field to grid带有 RIA 的 Silverlight 4 - 将连接的表字段绑定到网格
【发布时间】:2012-05-22 19:56:40
【问题描述】:

我有 2 个具有 1-1 关系的表 我正在尝试使用 .Include() for LINQ 尝试将子表实体传递给属性,以便我可以将子表字段以及父表中的字段绑定到网格。最初它像这样工作得很好,我能够绑定到网格,但只能从 BUGroupBuildings 表中获取结果。我还需要绑定到 vwBuisnessUnits 表中的一个字段。

 public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
    {
        var result = from d in this.ObjectContext.BUGroupBuildings
                     join b in this.ObjectContext.vwBusinessUnits on d.BU equals b.BU
                     where d.BUGroupID == i
                     orderby d.BU ascending
                     select d;

        return result;

    }

当我切换到使用 Include to bring back child table fields 时,出现错误

  public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
    {
        var result = from d in this.ObjectContext.BUGroupBuildings
                     .Include("vwBuisnessUnits")
                     select d;
        result = result.Where(w => w.BUGroupID == i).OrderBy(o => o.vwBusinessUnit.BU);

        return result;

    }

错误:

查询“GetBusinessUnitsBasedOnGroupID”的加载操作失败。一种 指定的包含路径无效。实体类型 “EQUITYDWModel.BUGroupBuilding”未声明导航属性 名称为“vwBuisnessUnits”

这是我的实体

我已在元数据中添加了必要的 [Include]。

    [MetadataTypeAttribute(typeof(BUGroupBuilding.BUGroupBuildingMetadata))]
public partial class BUGroupBuilding
{

    internal sealed class BUGroupBuildingMetadata
    {

        // Metadata classes are not meant to be instantiated.
        private BUGroupBuildingMetadata()
        {
        }

        public string BU { get; set; }
        [Include]
        public BUGroup BUGroup { get; set; }

        public int BUGroupBuildingsID { get; set; }

        public Nullable<int> BUGroupID { get; set; }
        [Include]
        public vwBusinessUnit vwBusinessUnit { get; set; }

    }
}

【问题讨论】:

  • 如果您的导航属性是 vwBusinessUnit,为什么还要使用 .Include("vwBuisnessUnits")。拼写错误?
  • vwBusinessUnits 是我的子表名。我想在 Inlcude 中使用导航名称吗?
  • 我更新为 Include("vwBusinessUnit") 但查询“GetBusinessUnitsBasedOnGroupID”的加载操作失败。指定的包含路径无效。 EntityType 'EQUITYDWModel.BUGroupBuilding' 未声明名为 'vwBuisnessUnit' 的导航属性
  • Include() 中有错字,vwBuisnessUnit 应该是 vwBusinessUnit。

标签: c# wpf silverlight silverlight-4.0 ria


【解决方案1】:

您是从 db 生成 EntityModel 还是手动创建它?您是否手动创建元数据类?

也许有些是错误的。应该在客户端(Web.g.cs 文件)上创建一个导航属性,如下所示:

/// <summary>
        /// Gets or sets the associated <see cref="tblCustomer"/> entity.
        /// </summary>
        [Association("tblCustomer_tblInvoice", "uiCustomerId", "Id", IsForeignKey=true)]
        [XmlIgnore()]
        public tblCustomer tblCustomer
        {
            get
            {
                if ((this._tblCustomer == null))
                {
                    this._tblCustomer = new EntityRef<tblCustomer>(this, "tblCustomer", this.FiltertblCustomer);
                }
                return this._tblCustomer.Entity;
            }
            set
            {
                tblCustomer previous = this.tblCustomer;
                if ((previous != value))
                {
                    this.ValidateProperty("tblCustomer", value);
                    if ((previous != null))
                    {
                        this._tblCustomer.Entity = null;
                        previous.tblInvoices.Remove(this);
                    }
                    if ((value != null))
                    {
                        this.uiCustomerId = value.Id;
                    }
                    else
                    {
                        this.uiCustomerId = default(Guid);
                    }
                    this._tblCustomer.Entity = value;
                    if ((value != null))
                    {
                        value.tblInvoices.Add(this);
                    }
                    this.RaisePropertyChanged("tblCustomer");
                }
            }
        }

请检查实体模型和关系。

【讨论】:

    【解决方案2】:

    啊。我找到了解决我的问题的方法。以为我会与社区分享。 事实证明,当我在 2 个实体(在我的情况下是视图和表)之间有自定义关系时,我必须在 linq 查询中进行连接和 Inlcude。在模型中定义关系时,不需要显式连接。

      public IQueryable<BUGroupBuilding> GetBusinessUnitsBasedOnGroupID(int i)
        {
            var result = from d in this.ObjectContext.BUGroupBuildings
                         join b in this.ObjectContext.vwBusinessUnits on d.BU equals b.BU
                         where d.BUGroupID == i
                         orderby d.BU ascending
                         select d;
    
    
            var r2 = from d2 in ((ObjectQuery<BUGroupBuilding>)result)
                     .Include("vwBusinessUnit")
                     select d2;
    
            return r2;            
        }
    

    【讨论】:

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