Answered by:
MetaChildrenColumn not created when using a DomainService with a one to Many Mapping

Question
-
User-1637241392 posted
I am using the Entity Framework and a Domain Service and I have a one-to-many mapping between two tables
I am trying to use the Dynamic Child grid example by Steve Naughton but My Data column is an EntityCollection rathen than a MetaChildrenColumn.
Has anyone got an equivalent solution
var metaChildColumn = Column as MetaChildrenColumn; (this is where it is falling over as my column is an EntityCollection)
var metaForeignKeyColumn = metaChildColumn.ColumnInOtherTable as MetaForeignKeyColumn; if (metaChildColumn != null && metaForeignKeyColumn != null) { GridDataSource.DomainServiceTypeName = typeof(DomainService).FullName;
MetaTable table = metaChildColumn.ChildTable;If I just try and use the EntityCollection then it is strongly typed to the actual child entity and I am looking for a more generic solution.
Has anyone come across this and found an equivalent solution?
Thanks
Friday, September 20, 2013 6:32 AM
Answers
-
User-1637241392 posted
Hi there, I have found a generic way of doing this though so far only for Viewing - I have not tried updating the gridview yet.
This is how I managed to get it to work.
public partial class ChildGrid : FieldTemplateUserControl { protected MetaTable DisplayTable { get; set; } private String[] DisplayColumns { get; set; } private String DisplayTableName { get; set; } protected void Page_Init(object sender, EventArgs e) { var attribute = Column.Attributes.OfType<ShowChildColumnsAttribute>().SingleOrDefault(); if (attribute != null) { if (attribute.DisplayColumns.Length > 0) DisplayColumns = attribute.DisplayColumns; DisplayTableName = attribute.TableName; } DisplayTable = Column.Table.Model.GetTable(DisplayTableName); GridView1.ColumnsGenerator = new FieldTemplateRowGenerator(DisplayTable, DisplayColumns); // setup the GridViewDataKeys var keys = new String[DisplayTable.PrimaryKeyColumns.Count]; int i = 0; foreach (var keyColumn in DisplayTable.PrimaryKeyColumns) { keys[i] = keyColumn.Name; i++; } GridView1.DataKeyNames = keys; GridView1.SetMetaTable(DisplayTable); } protected override void OnDataBinding(EventArgs e) { // Get the real entity from the wrapper object entity = Row; // Get the collection of reservations for this customer and make sure it's loaded var entityCollection = (RelatedEnd) Column.EntityTypeProperty.GetValue(entity, null); entityCollection.Load(); var objectQuery = (ObjectQuery) entityCollection.GetType().GetMethod( "CreateSourceQuery").Invoke(entityCollection, null); GridView1.DataSource = objectQuery; } // use this to navigate to the Details page for our Child Grid // gets the primary keys from the grid datakeys and uses these to // navigate to the details page for the table in question protected void GridSelectedIndexChanged(object sender, EventArgs e) { // Get the currently selected row using the SelectedRow property. string primarykeyvalues = string.Empty; // now let's build up the primary keys for the redirect if (GridView1.SelectedDataKey != null) { var keys = GridView1.SelectedDataKey.Values; foreach (DictionaryEntry key in keys) { primarykeyvalues = primarykeyvalues + key.Key; primarykeyvalues = primarykeyvalues + "=" + key.Value + "&"; } primarykeyvalues = primarykeyvalues.TrimEnd('&', ' '); } Response.Redirect("../" + DisplayTable + "/DetailsSubGridViews.aspx?" + primarykeyvalues); }
This seems to work quite nicely so far...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 26, 2013 11:10 AM
All replies
-
User-330204900 posted
Hi Sophie, (I tried to e-mail you but i am in france at the moment ad can't send for some reason.), sorry I don’t as MS have not adding the insert and update parameters to the DomainServiceDataSource, I may have a way of getting it to work but not had the time to try it out.
Just in case you want to try yourself I was thinking of adding on inserting and on updating event to the child grid see the EF ManyToMany_Edit field template for how to do that and then add the parameters there J
Saturday, September 21, 2013 3:09 PM -
User-1637241392 posted
Hi there, I have found a generic way of doing this though so far only for Viewing - I have not tried updating the gridview yet.
This is how I managed to get it to work.
public partial class ChildGrid : FieldTemplateUserControl { protected MetaTable DisplayTable { get; set; } private String[] DisplayColumns { get; set; } private String DisplayTableName { get; set; } protected void Page_Init(object sender, EventArgs e) { var attribute = Column.Attributes.OfType<ShowChildColumnsAttribute>().SingleOrDefault(); if (attribute != null) { if (attribute.DisplayColumns.Length > 0) DisplayColumns = attribute.DisplayColumns; DisplayTableName = attribute.TableName; } DisplayTable = Column.Table.Model.GetTable(DisplayTableName); GridView1.ColumnsGenerator = new FieldTemplateRowGenerator(DisplayTable, DisplayColumns); // setup the GridViewDataKeys var keys = new String[DisplayTable.PrimaryKeyColumns.Count]; int i = 0; foreach (var keyColumn in DisplayTable.PrimaryKeyColumns) { keys[i] = keyColumn.Name; i++; } GridView1.DataKeyNames = keys; GridView1.SetMetaTable(DisplayTable); } protected override void OnDataBinding(EventArgs e) { // Get the real entity from the wrapper object entity = Row; // Get the collection of reservations for this customer and make sure it's loaded var entityCollection = (RelatedEnd) Column.EntityTypeProperty.GetValue(entity, null); entityCollection.Load(); var objectQuery = (ObjectQuery) entityCollection.GetType().GetMethod( "CreateSourceQuery").Invoke(entityCollection, null); GridView1.DataSource = objectQuery; } // use this to navigate to the Details page for our Child Grid // gets the primary keys from the grid datakeys and uses these to // navigate to the details page for the table in question protected void GridSelectedIndexChanged(object sender, EventArgs e) { // Get the currently selected row using the SelectedRow property. string primarykeyvalues = string.Empty; // now let's build up the primary keys for the redirect if (GridView1.SelectedDataKey != null) { var keys = GridView1.SelectedDataKey.Values; foreach (DictionaryEntry key in keys) { primarykeyvalues = primarykeyvalues + key.Key; primarykeyvalues = primarykeyvalues + "=" + key.Value + "&"; } primarykeyvalues = primarykeyvalues.TrimEnd('&', ' '); } Response.Redirect("../" + DisplayTable + "/DetailsSubGridViews.aspx?" + primarykeyvalues); }
This seems to work quite nicely so far...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, September 26, 2013 11:10 AM