Here is the map for my two database tables. One is a category and the others are items in that category:
<Table Name="dbo.onTheMenu" Member="onTheMenu">
<Type Name="OnTheMenu">
<Column Name="ID" Member="ID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert"/>
<Column Name="itemName" Member="itemName" DbType="nvarchar(MAX)" CanBeNull="false"/>
<Column Name="itemImagePath" Member="itemImagePath" DbType="nvarchar(MAX)" CanBeNull="false"/>
<Column Name="itemDescription" Member="itemDescription" DbType="nvarchar(MAX)" CanBeNull="false"/>
<Column Name="itemPrice" Member="itemPrice" DbType="decimal(3, 2)" CanBeNull="false"/>
<Column Name="itemOrderNumber" Member="itemOrderNumber" DbType="nvarchar(10)" CanBeNull="false"/>
<Column Name="specialForDay" Member="specialForDay" DbType="nvarchar(9)" CanBeNull="false"/>
<Column Name="itemCategory" Member="itemCategory" DbType="Int" CanBeNull="false"/>
<Association Name="Menu_Children" Member="Children" Storage="_menuChildren" ThisKey="itemCategory" OtherKey="menuCategories.ID" IsForeignKey="true" />
</Type>
</Table>
<Table Name="dbo.menuCategories" Member="menuCategories">
<Type Name="MenuCategories">
<Column Name="ID" Member="ID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert"/>
<Column Name="menuCategory" Member="menuCategory" DbType="nvarchar(100)" CanBeNull="false"/>
<Column Name="menuDescription" Member="menuDescription" DbType="nvarchar(MAX)" CanBeNull="true"/>
<Association Name="Menu_Parent" Member="Parent" Storage="_menuParent" ThisKey="ID" IsForeignKey="true" />
</Type>
</Table>
This is the datacontext:
public Table<OnTheMenu> MenuItems { get { return this.GetTable<OnTheMenu>(); } }
public Table<MenuCategories> MenuCategories { get { return this.GetTable<MenuCategories>(); } }
And these are the entity classes:
public class OnTheMenu
{
public string itemName;
public string itemImagePath;
public string itemDescription;
public string itemPrice;
public int itemCategory;
public string itemOrderNumber;
public string specialForDay;
}
public class MenuCategories
{
public int ID;
public string menuCategory;
public string menuDescription;
private EntityRef<MenuCategories> _menuParent = default(EntityRef<MenuCategories>);
private EntitySet<OnTheMenu> _menuChildren = new EntitySet<OnTheMenu>();
public MenuCategories Parent
{
// return the current entity
get { return this._menuParent.Entity; }
set { this._menuParent.Entity = value; }
}
public EntitySet<OnTheMenu> Children
{
get { return this._menuChildren; }
set { this._menuChildren.Assign(value); }
}
}
And this is my View:
<% foreach (var menuCat in Model) { %>
<p>
<%= menuCat.menuCategory%></p>
<% foreach (var menuItem in menuCat.Children) {%>
<p>
<%= menuItem.itemName%></p>
<% } %>
<%} %>
The issue I’m having is that I can retrieve the categories using this but there are no children returned (each category’s Children .Count() is equal to zero). I know I’m either overlooking something simple or going about this the entirely wrong way. Do I have the associations correct?