C# 理解代码优先虚拟属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15247614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:39:36  来源:igfitidea点击:

Understanding code first virtual properties

c#entity-frameworkentity-framework-4.1

提问by aleczandru

Hi I am just learning to work with Entity Framework Code First and I can not seem to understand something.I have created three models based on a tutorial:

嗨,我刚刚学习使用 Entity Framework Code First,我似乎无法理解某些东西。我根据教程创建了三个模型:

public class Course
{
    public int CourseID { get; set; }
    public string Title { get; set; }
    public int Credits { get; set; }
    public virtual ICollection<Enrollment> Enrollments{ get; set; }
}

public class Enrollment
{
    public int EnrollmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }
    public decimal? Grade { get; set; }
    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}
public class Student
{
    public int StudentID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
    public DateTime EnrollmentDate { get; set; }
    public virtual ICollection<Enrollment> Enrollments { get; set; }
}

My problem is that I do not understand what the properties with virtual do.If I check the database there is no column crate for each of the properties , only for the others.

我的问题是我不明白 virtual 的属性是做什么的。如果我检查数据库,每个属性都没有列板条箱,只有其他属性。

So what happens when you create a property with the virtual keyword?

那么当您使用 virtual 关键字创建属性时会发生什么?

采纳答案by ken2k

It is used to manage lazy loadingand change tracking.

它用于管理延迟加载更改跟踪

EF will generate proxy types on runtime, which are dynamically generated types that inherit from your POCO classesand add all the EF stuff to manage lazy loading / change tracking in the overridden virtual properties.

EF 将在运行时生成代理类型,这些类型是从您的 POCO 类继承的动态生成的类型,并添加所有 EF 内容以管理覆盖的虚拟属性中的延迟加载/更改跟踪。

So virtualis not a "magic keyword" here, virtualis here so your POCOs can be inherited with additional EF-related code at runtime.

所以virtual这里不是一个“魔法关键字”,在这里virtual是为了让你的 POCO 可以在运行时用额外的 EF 相关代码继承。

回答by Freeman

When you create a property or method marked with the virtual keyword, you will be allowed to override it in a derived class, thus offering your method a more specialized behaviour depending on the objects you create.

当您创建一个用 virtual 关键字标记的属性或方法时,您将被允许在派生类中覆盖它,从而根据您创建的对象为您的方法提供更专业的行为。

In the case of Entity Framework its also a convention that points out that lazy loading behaviour is used. A question regarding this matter exists here: Entity Framework 4.1 Virtual Properties

在实体框架的情况下,它也是一个约定,指出使用延迟加载行为。此处存在有关此问题的问题:Entity Framework 4.1 Virtual Properties

回答by gregjer

Virtual properties are there to allow lazy loading

虚拟属性允许延迟加载