[英]Silverlight 4 with RIA - Binding joined table field to grid
I have 2 tables that has a 1-1 relationship I am attempting to use the .Include() for LINQ to try to pass a child table entity to a property so that I can bind a child table field to a grid, along with fields from the parent table. 我有2个表有1-1的关系我试图使用.Include()为LINQ尝试将子表实体传递给属性,以便我可以将子表字段绑定到网格,以及字段来自父表。 Originally it works fine like this and I'm able to bind to the grid but only get results from the BUGroupBuildings table. 最初它的工作原理很好,我可以绑定到网格,但只能从BUGroupBuildings表中获得结果。 I also need to bind to a field in the vwBuisnessUnits table. 我还需要绑定到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;
}
When I switch over to use the Include to bring back child table fields , I get an error 当我切换到使用Include 带回子表字段时 ,我收到一个错误
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;
}
ERROR: 错误:
Load operation failed for query 'GetBusinessUnitsBasedOnGroupID'. 查询'GetBusinessUnitsBasedOnGroupID'的加载操作失败。 A specified Include path is not valid. 指定的包含路径无效。 The EntityType 'EQUITYDWModel.BUGroupBuilding' does not declare a navigation property with the name 'vwBuisnessUnits' EntityType'EQUITYDWModel.BUGroupBuilding'未声明名为'vwBuisnessUnits'的导航属性
Here's my entity 这是我的实体
I've added the necessary [Include] in the metadata. 我在元数据中添加了必要的[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; }
}
}
Did you generate the EntityModel from db or create it manually? 您是从db生成EntityModel还是手动创建它? Are you create a metada class manually? 你手动创建一个metada类吗?
Maybe some are wrong. 也许有些是错的。 Should be create a navigation property on client side (Web.g.cs file) like this: 应该在客户端(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");
}
}
}
Please check the Entity Model and a relationships. 请检查实体模型和关系。
Ahhh. 唉唉。 I found the solution to my problem. 我找到了解决问题的方法。 Thought I would share it with the community. 以为我会与社区分享。 It turns out that I have to do a join and an Inlcude in the linq query when I have a custom relationship between 2 entities (in my case, a view and a table). 事实证明,当我在2个实体(在我的例子中是视图和表)之间有自定义关系时,我必须在linq查询中进行连接和Inlcude。 When a relationship is defined in the model, it doesn't require an explicit join. 在模型中定义关系时,它不需要显式连接。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.