Entity Framework Interview Questions

  1. What are the different approaches provided by Entity Framework?
    There are three different approaches provided by Entity Framework.
    Code First
    Model First
    Entity First
  2. Using Code First model, how can you explicitly mark a field/property as the primary key.
    By Applying attribute [Key]
  3. How do you explicitly enable foreign key relationship from another table?
    The solution is to define a navigation property for Task and apply annotation to mark the property as the Foreign Key.
    // This is inferred as Foreign key to project table
    public int EmployeeId { get; set; }
    // Explicitly define the Foreign Key — Known as Navigation Property
    [ForeignKey(“EmployeeId “)]
    public virtual Employee Employee { get; set; }
  4. How do you mark a property as required?
    By applying attribute [Required]
  5. How do you restrict length of fields/properties?
    By applying [MaxLength] and [MinLength] attributes.
    [MaxLength(400, ErrorMessage=”Maximum of 400 characters please”)]
    [MinLength(10, ErrorMessage=”Minimum of 10 characters required”)]
  6. How do you stop a column no to be created in database?
    By applying attribute [NotMapped]
  7. How do you specify name of table and column in Entity Framework?
  8. how can you instruct Entity Framework to automatically compute and insert a column in database?
    By applying below attribute.
  9. How do you enable/disable proxy creation and lazy loading?
    this.Configuration.ProxyCreationEnabled = false; //true
    this.Configuration.LazyLoadingEnabled = false; //true
    Based on above flag values, below is the configuration applied.
    proxy creation true and lazy loading true => change tracking & lazy loading
    proxy creation true and lazy loading false => change tracking & No lazy loading
    proxy creation false and lazy loading true => No change tracking &  No lazy loading
  10. How do you save changes to database?
    By calling SaveChanges() of DbContext object.
  11. What makes Entity Framework slower?
    Inappropriate use of Lazy loading
  12. How can you disable lazy loading of related entities accessed via navigation properties?
    By removing the virtual keyword for navigation property.
  13. How do you perform Eager loading?
    // load all employees, tasks
    var data= context.Projects.Include(a => a.Tasks).ToList();// load all employees, tasks and projects
    var data= context.Employees.Include(e => e.TasksCreated.Select(t => t.Project)).ToList();
  14. What is lazy loading?
    Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database first time that a property referring to the entity/entities is accessed.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s