locked
ManyToMany field can't get the value RRS feed

  • Question

  • User-323784514 posted

    Hi,

    I have 2 models and they have a Property with ManyToMany relationship, I've put some sample data in my PostgreSql database related tables which created by EntityFramework6, and can correctly see the json format data via a ASP.NET WEB API application, so this test indicates the data are all correct.

    Later I want show and support editing for these models in a web page, so I choose the ASP.NET Dynamic Data, the problem is that ManyToMany field always empty in List page, by set a break point to Field template of ManyToMany.ascx, I can see the entityCollection always null via:

    var entityCollection = Column.EntityTypeProperty.GetValue(entity, null);

    the more detail question is posted at:

    http://stackoverflow.com/questions/39952062/cant-retrieve-manytomany-field-value-in-asp-net-dynamic-data

    could you help?

    Monday, October 10, 2016 3:18 PM

All replies

  • User-330204900 posted

    Hi Shawn, what project template are you using and what version of Visual Studio are you on?

    Monday, October 10, 2016 4:08 PM
  • User-323784514 posted

    it's vs2015 community version, the project template is the 'ASP.NET Dynamic Data Entities Web Application'

    I believe the template I installed is from https://visualstudiogallery.msdn.microsoft.com/9402d38e-2a85-434e-8d6a-8fc075068a42   which published as a top most post in this forum,  and I just manually un-zipped the MicrosoftAspNetTemplates.vsix  , I can see the template code matches mine.

    Monday, October 10, 2016 11:08 PM
  • User-323784514 posted

    And one more thing may off the topic, I noticed one line of code obviously missed a null check in ManyToMany_Edit.ascx.cs, and always cause an exception:

            private static bool ListContainsEntity(MetaTable table, IEnumerable<object> list, object entity)
            {
                // the below null check was not existed in original template code, and this is necessary, otherwise, if Model's ManyToMany property
    // is not filled with data, then exception will popup.
    //if (list == null) return false; return list.Any(e => AreEntitiesEqual(table, e, entity)); }



    Tuesday, October 11, 2016 1:43 AM
  • User-330204900 posted

    OK then that is all good for this to work, so I will have to say there must be something in your model that is causing this. Can you post the two entities that are in this relationship please.

    Tuesday, October 11, 2016 9:42 AM
  • User-323784514 posted

    I've posted all details on that link to stackoverflow.com, i'll copy the definition of models again:

    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; }
    }

    Tuesday, October 11, 2016 12:33 PM
  • User-330204900 posted

    OK I've asked someone who knows EF6 and Code First better then me to have a look.

    Tuesday, October 11, 2016 12:59 PM
  • User-323784514 posted

    Any progress on this?

    thx!

    Wednesday, October 12, 2016 1:46 PM
  • User-707554951 posted

    Hi shawn.shao,
    [by debugging, I can see the entityCollection always null, anything I've missed?]
    According your description in stackoverflow, I suggest you could to the check whether there is data in rowDescriptor.GetPropertyOwner(null) and Row through debugging your code.
    Best regards
    Cathy

    Friday, October 14, 2016 6:01 AM
  • User-323784514 posted
    public partial class ManyToManyField : System.Web.DynamicData.FieldTemplateUserControl
    {
            protected override void OnDataBinding(EventArgs e)
            {
                base.OnDataBinding(e);
    
                object entity;
                //!!!! Row is not null, type is: Microsoft.AspNet.EntityDataSource.EntityDataSourceWrapper
                ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
                //!!!! rowDescriptor is not null
                if (rowDescriptor != null)
                {
                    //!!!! entity is not null, type is: PosTransactionModel
                    entity = rowDescriptor.GetPropertyOwner(null);
                }
                else
                {
                    entity = Row;
                }
    
                //!!!! entityCollection is null, and Column = {MetaChildrenColumn SaleItems} and Column.EntityTypeProperty = {System.Collections.Generic.ICollection`1[SharedModel.PosItemModel] SaleItems}
                var entityCollection = Column.EntityTypeProperty.GetValue(entity, null);
                var realEntityCollection = entityCollection as RelatedEnd;
                if (realEntityCollection != null && !realEntityCollection.IsLoaded)
                {
                    realEntityCollection.Load();
                }
    
                Repeater1.DataSource = entityCollection;
                Repeater1.DataBind();
            }

    see the code comments to see the debug info.

    additional, by set a break point here in first Default page, I always can see the `firstTrx` with all property data correctly loaded:

    public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ApplicationDbContext db = new ApplicationDbContext();
                var firstTrx = db.PosTransactionModels.Include(t => t.SaleItems).ToList().First();
                Console.WriteLine(firstTrx.ToString());

    Friday, October 14, 2016 1:50 PM
  • User-323784514 posted

    any progress?

    thanks!

    Thursday, October 20, 2016 8:06 AM
  • User-330204900 posted

    I have asked someone to have a look but not heard anything back I will ask again :)

    Thursday, October 20, 2016 9:12 AM