0

I am EF new learner, i have some questions to ask:

1. I don't understand what does virtual mean in for example one to many, i give an example: one question can have many options for e.g.

public class Question
{
    public int QuestionId { get; set; }
    public string Title { get; set; }
    public virtual List<Option> Options { get; set; }
}

public class Option
{
    public int OptionId { get; set; }
    public string OptionText { get; set; }
    public virtual Question Question { get; set; }
}

but what does the "2" virtual mean, because if i delete the virtual in this line: public virtual List<Option> Options { get; set; }, i didn't find any differences, it works well as one to many, so can you explain me very clearly and easily what the 2 virtual mean, if it's override, override what?

2. I don't know when we should use API fluent, for e.g. the previous one to many ex. without api fluent, it's still a one to many Relationship, so please just tell me when we should use it ? in which occasions for example.

3. in the API fluent, i know "withmany" and "hasmany" together, they mean "many to many", and what's "withrequired? isoptional? "

Videl
  • 840
  • 2
  • 7
  • 18

1 Answers1

0

I think I can answer following points:

  1. EF define all navigation property virtual, so that EF will at runtime create a new class (dynamic proxy) derived from your Brand class and use it instead. This new dynamically created class contains logic to load navigation property when accessed for the first time. This feature is called lazy loading (or better transparent lazy loading).

See the following link for more detail

Entity Framework 4.1 Virtual Properties

2.When to use fluent API

When working with Code First, you define your model by defining your domain CLR classes. By default, the Entity Framework uses the Code First conventions to map your classes to the database schema. If you use the Code First naming conventions, in most cases you can rely on Code First to set up relationships between your tables based on the foreign keys and navigation properties that you define on the classes. If you do not follow the conventions when defining your classes, or if you want to change the way the conventions work, you can use the fluent API or data annotations to configure your classes so Code First can map the relationships between your tables.

http://msdn.microsoft.com/en-us/data/jj591620.aspx

3.See the following link to know What is .WithRequired and .WithOptional options

What part does the .WithRequired play in an EF Fluent API?

Community
  • 1
  • 1
Imran Rizvi
  • 7,331
  • 11
  • 57
  • 101