0

给定域表结构,我试图让我的模型填充。

我有一个存储供应商的主表,每个供应商都可以从主类别查找表中选择一对多的类别。供应商选择存储在另一个表中,该表仅存储 VendorID 和 CategoryID 以在它们之间进行链接。

我可以编写查询(如下)以包含类别表,但我只能显示 categoryID 而不是类别名称。

        public VendorProfile GetVendor(String id)
    {
        Guid pid = Guid.Parse(id);
        var view = _db.VendorProfiles.Include("VendorCategories").Single(v => v.ProfileID == pid);
        return view;
    }

我试图在查询中包含查找表(如下),但这会产生运行时错误。

        public VendorProfile GetVendor(String id)
    {
        Guid pid = Guid.Parse(id);
        var view = _db.VendorProfiles.Include("VendorCategories").Include("ProductServiceCategory").Single(v => v.ProfileID == pid);
        return view;
    }

指定的包含路径无效。EntityType 'VendorProfilesIntranet.VendorProfile' 没有声明名为 'ProductServiceCategory' 的导航属性。

Category 表确实具有导航属性。我不知道如何将相同的导航属性添加到主表,因为它没有任何 FK 到查找表。

更新:

@Gert 这个符号确实有效!

_db.VendorProfiles.Include("VendorCategories.ProductServiceCategory").Single(v => v.ProfileID == pid);

但是,我现在显示的只是选择的类别项目。我希望获得完整的类别列表并检查已选择的类别。我正在使用滚动复选框列表。

       <div class="scroll_checkboxes">
        <ul>
        @foreach (var c in Model.VendorCategories)
        {
           <li>
               <input type="checkbox" name="categories" value="@c.ID" /> @c.ProductServiceCategory.CategoryName
           </li>
        }
        </ul>
        @Html.ValidationMessage("Please check at least one Product/Service category")
    </div>

更新2:

可能有更好的解决方案,但对于任何陷入类似情况的人来说,这行得通

        <div class="scroll_checkboxes">
        <ul>
        @foreach (var c in ViewBag.Categories)
        {
           <li>
               @foreach(var vc in Model.VendorCategories)
               {
                   if(c.Id == vc.CategoryID)
                   {
                        <input type="checkbox" name="categories" checked="checked" value="@c.Id" /> @vc.ProductServiceCategory.CategoryName
                       <br />
                   }
                   else
                   {
                        <input type="checkbox" name="categories" value="@c.Id" /> @vc.ProductServiceCategory.CategoryName
                        <br />
                   }
               }
           </li>
        }
        </ul>
        @Html.ValidationMessage("Please check at least one Product/Service category")
    </div>
4

1 回答 1

1

你可以做

_db.VendorProfiles.Include("VendorCategories.ProductServiceCategory")

在结果集中同时包含 theVendorCategories和它们ProductServiceCategory的 s。

顺便说一句,如果您使用DbExtensions.Include智能感知来帮助您找到正确的包含路径。

于 2012-11-15T22:00:00.590 回答