【发布时间】:2016-10-10 05:57:42
【问题描述】:
我有 2 个模型 PosTransactionModel 和 PosItemModel,它们通过 EntityFramework6 基于 Postgresql 构建,它们将每个模型引用为 ManyToMany 关系。
public class PosTransactionModel
{
public int Id { get; set; }
public ICollection<PosItemModel> SaleItems { get; set; }
...
...
}
public class PosItemModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ItemId { get; set; }
public ICollection<PosTransactionModel> SoldInPosTransactions { get; set; }
}
我可以看到已经成功创建了 3 个表来维护数据和关系:
在数据库表中填写了一些测试数据,我可以看到PosTransactionModel及其SaleItems数据可以通过WebAPI接口正确发布,客户端测试http客户端可以得到正确的JSON数据:
现在,我正在尝试构建一个管理站点以通过 ASP.NET 动态数据模板管理这些数据,问题是 ManyToMany SaleItems 字段总是针对测试数据为空:
这是ManyToManyField的默认模板代码:
public partial class ManyToManyField : System.Web.DynamicData.FieldTemplateUserControl
{
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
object entity;
ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
if (rowDescriptor != null)
{
entity = rowDescriptor.GetPropertyOwner(null);
}
else
{
entity = Row;
}
var entityCollection = Column.EntityTypeProperty.GetValue(entity, null);
var realEntityCollection = entityCollection as RelatedEnd;
if (realEntityCollection != null && !realEntityCollection.IsLoaded)
{
realEntityCollection.Load();
}
Repeater1.DataSource = entityCollection;
Repeater1.DataBind();
}
public override Control DataControl
{
get
{
return Repeater1;
}
}
}
通过调试,我可以看到entityCollection 始终为空,我错过了什么?
【问题讨论】:
标签: c# asp.net entity-framework asp.net-dynamic-data