简体   繁体   English

为什么我需要在子类中使用EF5的虚拟属性?

[英]Why do I need to have a virtual property with EF5 in my child class?

I am using Entity Framework 5 with Code First and lazy loading disabled. 我正在使用Entity Framework 5与Code First和延迟加载禁用。 Given the following: 鉴于以下内容:

public class Department
{
    public int DepartmentID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Course> Courses { get; set; }
}

public class Course
{
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int Credits { get; set; }
    public int DepartmentID { get; set; }
    public virtual Department Department { get; set; }
}

// Mapping Relationships for Course

<code here>
this.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID);

Can someone explain why I would need to have the: 有人可以解释为什么我需要有:

public virtual Department Department { get; set; }

What would happen if I completely remove this line ? 如果我完全删除这条线会怎么样? What would I not be able to do? 我什么都做不到? As far as I can see the only thing I could not do is to use Entity Framework to check my foreign key. 据我所知,我唯一不能做的是使用Entity Framework来检查我的外键。 However I already have this set up in the database so I would still get an error from the Db. 但是我已经在数据库中设置了这个,所以我仍然会从Db中收到错误。

The Department property exists as a link to a specific department for a course. Department属性作为指向课程特定部门的链接存在。 It can be part of a one to many relationship which gives you access to a department from a course. 它可以是一对多关系的一部分,使您可以从课程访问部门。

It contains your Department information, Depertment Name Courses information.I think you can remove it because the information of Department you can also access by searching for its DepartmentID . 它包含您的Department信息, Depertment Name Courses信息。我认为您可以删除它,因为Department的信息您也可以通过搜索其DepartmentID来访问。 But it would be better to keep it. 但保留它会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM