带有RIA的Silverlight 4 - 将表格字段绑定到网格

时间:2012-05-22 19:56:40

标签: c# wpf silverlight silverlight-4.0 ria

我有2个表有1-1的关系 我试图使用LINQ的.Include()来尝试将子表实体传递给属性,以便我可以将子表字段绑定到网格,以及父表中的字段。最初它的工作原理很好,我可以绑定到网格,但只能从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;

    }

当我切换到使用包含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'的加载操作失败。一个   指定的包含路径无效。 EntityType   'EQUITYDWModel.BUGroupBuilding'不声明导航属性   名称为“vwBuisnessUnits”

这是我的实体 enter image description here

我在元数据中添加了必要的[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; }

    }
}

2 个答案:

答案 0 :(得分:1)

您是从db生成EntityModel还是手动创建它?你手动创建一个metada类吗?

也许有些人错了。应该在客户端(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");
                }
            }
        }

请检查实体模型和关系。

答案 1 :(得分:1)

唉唉。我找到了解决问题的方法。以为我会与社区分享。 事实证明,当我在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;            
    }