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);
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));
}
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.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
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; }
}
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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());
None
0 Points
8 Posts
ManyToMany field can't get the value
Oct 10, 2016 03:18 PM|shawn.shao|LINK
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:
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?
All-Star
17916 Points
5681 Posts
MVP
Re: ManyToMany field can't get the value
Oct 10, 2016 04:08 PM|sjnaughton|LINK
Hi Shawn, what project template are you using and what version of Visual Studio are you on?
Always seeking an elegant solution.
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 10, 2016 11:08 PM|shawn.shao|LINK
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.
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 11, 2016 01:43 AM|shawn.shao|LINK
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:
All-Star
17916 Points
5681 Posts
MVP
Re: ManyToMany field can't get the value
Oct 11, 2016 09:42 AM|sjnaughton|LINK
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.
Always seeking an elegant solution.
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 11, 2016 12:33 PM|shawn.shao|LINK
I've posted all details on that link to stackoverflow.com, i'll copy the definition of models again:
All-Star
17916 Points
5681 Posts
MVP
Re: ManyToMany field can't get the value
Oct 11, 2016 12:59 PM|sjnaughton|LINK
OK I've asked someone who knows EF6 and Code First better then me to have a look.
Always seeking an elegant solution.
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 12, 2016 01:46 PM|shawn.shao|LINK
Any progress on this?
thx!
Star
8670 Points
2882 Posts
Re: ManyToMany field can't get the value
Oct 14, 2016 06:01 AM|Cathy Zou|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 14, 2016 01:50 PM|shawn.shao|LINK
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:
None
0 Points
8 Posts
Re: ManyToMany field can't get the value
Oct 20, 2016 08:06 AM|shawn.shao|LINK
any progress?
thanks!
All-Star
17916 Points
5681 Posts
MVP
Re: ManyToMany field can't get the value
Oct 20, 2016 09:12 AM|sjnaughton|LINK
I have asked someone to have a look but not heard anything back I will ask again :)
Always seeking an elegant solution.