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?
In the ASP.NET MVC framework, the code-first approach is a development model where you first write the code that creates the data access layer, then you write the code that creates the controllers and views. In the code-first approach, you create a model, which is a class that represents the data in the application.
The virtual keyword in C# enables a method or property to be overridden by child classes.
If you define your navigation property virtual , Entity Framework will at runtime create a new class (dynamic proxy) derived from your class and uses it instead of your original class. This new dynamically created class contains logic to load the navigation property when accessed for the first time.
Conventions namespace. You can further configure your model by using data annotations or the fluent API. Precedence is given to configuration through the fluent API followed by data annotations and then conventions.
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.
Virtual properties are there to allow lazy loading
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With