了解代码第一个虚拟属性 [英] Understanding code first virtual properties
问题描述
public class Course
{
public int CourseID {get;组; }
public string标题{get;组; }
public int Credits {get;组; }
public virtual ICollection<注册>注册{get;组;
}
public class注册
{
public int EnrollmentID {get;组; }
public int CourseID {get;组; }
public int StudentID {get;组; }
public decimal?年级{get;组; }
public virtual Course Course {get;组; }
public virtual Student Student {get;组; }
}
public class Student
{
public int StudentID {get;组; }
public string LastName {get;组; }
public string FirstMidName {get;组; }
public DateTime注册日期{get;组; }
public virtual ICollection<注册>注册{get;组; }
}
我的问题是我不明白虚拟的属性。如果我检查数据库,每个属性都没有列条形图,只有其他属性。
那么当您使用virtual关键字创建属性时会发生什么? / p>
它用于管理延迟加载和更改跟踪 / p>
EF将在运行时生成代理类型,它们是从您的POCO类继承的动态生成类型,并添加所有EF内容以管理延迟加载/更改跟踪在覆盖的虚拟属性。
所以 virtual
不是一个魔术关键字这里, virtual
在这里,所以您的POCO可以在运行时继承与额外的EF相关的代码。
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:
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.
So what happens when you create a property with the virtual keyword?
It is used to manage lazy loading and change tracking.
EF will generate proxy types on runtime, which are dynamically generated types that inherit from your POCO classes and add all the EF stuff to manage lazy loading / change tracking in the overridden virtual properties.
So virtual
is not a "magic keyword" here, virtual
is here so your POCOs can be inherited with additional EF-related code at runtime.
这篇关于了解代码第一个虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!