【问题标题】:MVC4 how to load related data without Navigation PropertiesMVC4如何在没有导航属性的情况下加载相关数据
【发布时间】:2013-01-31 00:44:21
【问题描述】:

我是 MVC 的新手,并且使用 EF-database-first 创建了一个 MVC4 应用程序。数据库不包含外键定义,我无法添加它们(我不拥有数据库)。以下是数据库中的两个示例类:

public partial class Allocation
{
    public int AllocID { get; set; }
    public int DeptID { get; set; }
    public decimal AllocationPercent { get; set; }
}

public partial class Department
{
    public int DeptID { get; set; }
    public string DeptName { get; set; }
    public string Account { get; set; }
}

默认分配索引页面显示部门 ID。我想显示部门名称。如果没有导航属性,我怎么能做到这一点?

我试过了

public class AllocationController : Controller
{
    private Entities db = new Entities();

    //
    // GET: /Allocation/

    public ActionResult Index()
    {
        return View(db.Allocation.Include(d => d.DeptID).ToList());
    }
...

但这会产生错误(“指定的包含路径无效。EntityType 'TESTModel.Allocation' 未声明名为 'DeptID' 的导航属性。”)...

我也不确定如何在没有导航属性的情况下编写预先加载或显式加载的代码,这引发了这个问题。效率方面,我认为加载相关信息的方式并不重要,因此我们将不胜感激任何方向的帮助。

【问题讨论】:

    标签: asp.net-mvc-4 entity-framework-5 ef-database-first


    【解决方案1】:

    数据库不必有定义,只要字段存在并且实体已在考虑到参照完整性的情况下放置在数据库中即可。您需要做的就是让实体框架知道关系。这是通过 virtual 关键字创建“导航属性”来完成的。

    public partial class Allocation
    {
     public int AllocID { get; set; }
     public int DeptID { get; set; }
     public decimal AllocationPercent { get; set; }
     public virtual Department Department { get; set; } /* this is your nav property */
    }
    
    public partial class Department
    {
     public int DeptID { get; set; }
     public string DeptName { get; set; }
     public string Account { get; set; }
    }
    

    现在你可以这样做了:

    db.Allocation.Include(a => a.Department).ToList()
    

    可能有一个错误需要您使用外键定义(尽管我不这么认为)。如果是这种情况,您将需要像这样装饰您的导航属性

    [ForeignKey("DeptID")]
    public virtual Department Department { get; set; }
    

    你也可以这样试试:

     public int AllocID { get; set; }
     [ForeignKey("Department")]
     public int DeptID { get; set; }
     public decimal AllocationPercent { get; set; }
     public virtual Department Department { get; set; }
    

    【讨论】:

    • 我添加了公共虚拟部门 Department { get;放; }
    • 感谢您的信息。这一定是答案,但我一直收到同样的错误......尝试将虚拟线添加到部分类(没有用);尝试将 ForeignKey 注释(以及必需的 using 指令)添加到第一个部分类,并且当这对元数据类不起作用时(这也不起作用)。现在我了解了虚拟,我将进行更多研究。
    • Department 类有一个复合键 [DeptID, DeptName] 会导致这个问题吗?我读到的所有其他内容都表明您的解决方案应该可以工作,但我仍然遇到相同的导航属性错误。
    • @jpg0002 - 也许你可以用你得到的错误文本问另一个问题。我相信您也可以尝试通过将外键定义放在 id 本身上并将其从虚拟字段中删除来反转外键定义。见编辑
    • 我尝试将装饰放在 DeptID 上作为您的编辑显示以及虚拟部门,并且在两种情况下都出现相同的错误。我将发布另一个问题,其中包含更多详细信息和确切的错误消息。谢谢你的帮助;我会将您的回复标记为答案。
    【解决方案2】:

    有了导航属性,Travis J 的答案就是你所需要的。 如果您不想使用导航属性,假设您的数据库上下文有一个名为Departments 的集合,您可以这样做:

    var deptId = db.Allocation.DeptID;
    var departments = db.Departments.Where(p => p.DeptID == deptId);
    return View(departments.ToList());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      相关资源
      最近更新 更多