无法在ASP.NET动态数据中检索ManyToMany字段值 [英] Can't retrieve ManyToMany field value in ASP.NET Dynamic Data

查看:72
本文介绍了无法在ASP.NET动态数据中检索ManyToMany字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型PosTransactionModelPosItemModel通过 EntityFramework6 建立在 Postgresql 上,它们分别引用为 ManyToMany 关系.

I have 2 models PosTransactionModel and PosItemModel which build on Postgresql via EntityFramework6, they reference each as a ManyToMany relationship.

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个表来维护数据和关系:

I can see 3 tables has been successfully created for maintain the data and relationship:

在数据库表中填充了一些测试数据,我可以看到PosTransactionModel及其SaleItems数据可以通过WebAPI接口正确发布,并且客户端测试http客户端可以获取正确的JSON数据:

with some testing data filled in database table, I can see the PosTransactionModel and its SaleItems data can be correctly published via a WebAPI interface, and client side testing http client can get the correct JSON data:

现在,我正在尝试建立一个管理站点,以通过ASP.NET动态数据模板管理这些数据,问题是 ManyToMany SaleItems 字段始终对测试数据为空:

Now, I'm trying to build an admin site for manage these data by ASP.NET Dynamic Data template, the problem is that ManyToMany SaleItems field always empty against the testing data:

这是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始终为空,我错过了什么?

by debugging, I can see the entityCollection always null, anything I've missed?

推荐答案

我认为问题出在您的数据模型上,我没有将EF6与DD一起使用,因为这对我来说有一些问题(在保存更改时运行业务逻辑)不适用于EF数据源),除了我可以想到的是,这与您的模型有关,因为M2M字段模板在EF5和EF6数据库优先模式下可以正常工作.

I think the issue will be with your data model I have not used EF6 with DD as there are some issues for me (running Business logic in the on saving changes does not work with EF Data source) that aside all I can think is it is something to do with your model as the M2M field template works fine in EF5 and EF6 Database First mode.

这篇关于无法在ASP.NET动态数据中检索ManyToMany字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆