MVC4如何在没有导航属性的情况下加载相关数据 [英] MVC4 how to load related data without Navigation Properties

查看:103
本文介绍了MVC4如何在没有导航属性的情况下加载相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I an fairly new to MVC, and have created an MVC4 application using EF-database-first. The database does not contain foreign key definitions and I can't add them (I don't own the database). Here are two example classes from the database:

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.我想显示部门名称.没有导航属性怎么办?

The default Allocation Index page shows the department ID. I want to show the department name instead. How can I do this without navigation properties?

我尝试了

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'的导航属性.")...

but this gives an error ("A specified Include path is not valid. The EntityType 'TESTModel.Allocation' does not declare a navigation property with the name 'DeptID'.")...

我不确定如何在没有导航属性的情况下对快速加载或显式加载进行编码,这提示了这个问题.从效率角度来看,我认为加载相关信息的方式并不重要,因此任何方向的帮助都将不胜感激.

I'm not sure how to code eager-loading or explicit-loading without navigation properties either, which prompted this question. Efficiency-wise, I don't believe it matters which way I load the related information, so any help in any direction would be appreciated.

推荐答案

数据库不必具有定义,只要其中存在字段且实体已经考虑到引用完整性就放置在数据库中即可.您需要做的就是让实体框架知道这种关系.这是通过virtual关键字创建导航属性"完成的.

The database does not have to have definitions, as long as the fields are there and the entities have been placed in the database with referential integrity in mind. All you need to do is let entity framework know about the relationship. This is done with the virtual keyword to create "Navigational Properties".

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()

可能会出现错误,要求您使用外键定义(尽管我不这么认为).在这种情况下,您需要像这样装饰您的导航属性

There may be an error which requires you to use a foreign key definition (although I do not think so). If this is the case, you will need to decorate your navigation property like this

[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; }

这篇关于MVC4如何在没有导航属性的情况下加载相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆