Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Entity Framework (EF)?
Entity Framework (EF) is an object-relational mapping (ORM) framework that enables developers to work with relational data using domain-specific objects.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 2. What is DbContext in Entity Framework?
DbContext is a class in EF that represents a session with the database, allowing you to query and save data.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 3. How can you disable Lazy Loading in Entity Framework?
You can disable Lazy Loading by setting the LazyLoadingEnabled property of the DbContext to false.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 4. How can you perform eager loading using Include method in Entity Framework?
The Include method is used to specify related entities that should be loaded along with the main entity. For example, context.Orders.Include(o => o.Customer).ToList();
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 5. What is the purpose of the AsNoTracking method in Entity Framework?
The AsNoTracking method is used to disable change tracking for a specific query, which can improve performance when you only need to read data.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 6. What is the purpose of the [Table] attribute in Entity Framework?
The [Table] attribute is used to specify the table name for an entity when it does not match the default convention.
Example:
[Table("MyCustomTableName")] public class MyEntity { /* properties */ }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 7. Explain the purpose of the [Key] attribute in Entity Framework.
The [Key] attribute is used to specify the primary key property of an entity when it does not match the default convention.
Example:
[Key] public int MyCustomId { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 8. Explain the purpose of the [NotMapped] attribute in Entity Framework.
The [NotMapped] attribute is used to specify that a property should not be mapped to a column in the database.
Example:
[NotMapped] public string CalculatedProperty { get { /* calculation logic */ } }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 9. What is the purpose of the DbSet.Find method in Entity Framework?
The DbSet.Find method is used to retrieve an entity from the database by its primary key value.
Example:
var entity = context.MyEntities.Find(1);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 10. What is the purpose of the [MaxLength] attribute in Entity Framework?
The [MaxLength] attribute is used to specify the maximum length of a string or binary property when creating the database schema.
Example:
[MaxLength(50)] public string Name { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 11. What is the purpose of the ToListAsync method in Entity Framework?
The ToListAsync method is used to asynchronously execute a query and return the result as a List.
Example:
var result = await context.Entities.ToListAsync();
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 12. What is the purpose of the [Required] attribute in Entity Framework?
The [Required] attribute is used to specify that a property is required and must have a non-null value when saving to the database.
Example:
[Required] public string Name { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 13. Explain the purpose of the [StringLength] attribute in Entity Framework.
The [StringLength] attribute is used to specify the maximum length of a string property when creating the database schema.
Example:
[StringLength(100)] public string Description { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 14. Explain the purpose of the Add method in Entity Framework.
The Add method is used to add a new entity to the context, marking it as Added, and inserting it into the database when SaveChanges is called.
Example:
context.Entities.Add(newEntity);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 15. What is the purpose of the Remove method in Entity Framework?
The Remove method is used to mark an entity as Deleted in the context, and the entity will be deleted from the database when SaveChanges is called.
Example:
context.Entities.Remove(entityToDelete);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 16. Explain the purpose of the AsQueryable method in Entity Framework.
The AsQueryable method is used to convert an IEnumerable Example:
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 17. What is the purpose of the Set method in Entity Framework?
The Set method is used to get a DbSet for a given entity type, allowing you to perform database operations on that entity type.
Example:
var set = context.Set();
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 18. Explain the purpose of the FindAsync method in Entity Framework.
The FindAsync method is used to asynchronously retrieve an entity from the database by its primary key value.
Example:
var entity = await context.MyEntities.FindAsync(1);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Intermediate / 1 to 5 years experienced level questions & answers
Ques 19. Explain Code First approach in Entity Framework.
Code First is an approach where you define your data model using C# or VB.NET classes, and the database is generated based on these classes.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 20. Differentiate between DbSet and DbContext in Entity Framework.
DbContext is a representation of a session with the database, while DbSet is a collection of entities that are used to perform database operations on those entities.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 21. Explain Lazy Loading in Entity Framework.
Lazy Loading is a feature in EF where related entities are not loaded from the database until they are explicitly accessed in code.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 22. What is Eager Loading in Entity Framework?
Eager Loading is the opposite of Lazy Loading. It loads the related entities along with the main entity in a single database query.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 23. What is the Database First approach in Entity Framework?
Database First is an approach where the data model is generated from an existing database, and the corresponding classes are created in the application.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 24. How can you execute raw SQL queries in Entity Framework?
You can use the SqlQuery method of the Database property in DbContext to execute raw SQL queries.
Example:
var result = context.Database.SqlQuery("SELECT * FROM MyTable");
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 25. Explain the concept of migrations in Entity Framework.
Migrations are a way to manage database schema changes over time. They allow you to evolve your database schema as your application evolves.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 26. Explain the purpose of the EntityState enumeration in Entity Framework.
EntityState is an enumeration that represents the state of an entity being tracked by the DbContext. It can be used to determine whether an entity is new, modified, or deleted.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 27. What is the purpose of the DatabaseGenerated attribute in Entity Framework?
The DatabaseGenerated attribute is used to configure how values for a property are generated by the database when inserting a new record.
Example:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 28. What is the purpose of the ExecuteSqlCommand method in Entity Framework?
The ExecuteSqlCommand method is used to execute a SQL command that does not return any results, such as an UPDATE or DELETE statement.
Example:
context.Database.ExecuteSqlCommand("UPDATE MyTable SET Column1 = 1 WHERE Id = 1");
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 29. Explain the purpose of the IDbSet interface in Entity Framework.
The IDbSet interface is an interface that represents a collection of entities in the context. It is implemented by DbSet.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 30. How can you execute stored procedures in Entity Framework?
You can execute stored procedures in EF using the context.Database.SqlQuery method or by creating a method on your DbContext that calls SqlQuery or ExecuteSqlCommand.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 31. Explain the purpose of the ChangeTracker in Entity Framework.
ChangeTracker is a property of DbContext that keeps track of the changes made to entities, allowing you to inspect and revert changes before saving to the database.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 32. Explain the purpose of the [ForeignKey] attribute in Entity Framework.
The [ForeignKey] attribute is used to specify the foreign key property in a relationship when the default convention is not followed.
Example:
[ForeignKey("CategoryId")] public int CategoryId { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 33. What is the purpose of the Entry method in Entity Framework?
The Entry method is used to access the EntityEntry object, which provides information about an entity being tracked by the context and allows you to perform actions like updating and deleting.
Example:
var entry = context.Entry(entity);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 34. Explain the purpose of the State property in Entity Framework.
The State property of EntityEntry is used to get or set the state of an entity being tracked by the context, such as Added, Modified, or Deleted.
Example:
entry.State = EntityState.Modified;
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 35. What is the purpose of the FromSqlRaw method in Entity Framework?
The FromSqlRaw method is used to create a query based on raw SQL and map the result to entities. It is often used for executing stored procedures.
Example:
var result = context.Entities.FromSqlRaw("EXEC dbo.GetEntities");
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 36. Explain the purpose of the IsDeleted property in Entity Framework.
The IsDeleted property is often used in soft delete scenarios where instead of actually deleting a record, a flag is set to mark it as deleted.
Example:
public bool IsDeleted { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 37. What is the purpose of the Attach method in Entity Framework?
The Attach method is used to reattach a disconnected entity to the context, allowing you to update it without having to retrieve it again from the database.
Example:
context.Entities.Attach(disconnectedEntity);
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Experienced / Expert level questions & answers
Ques 38. Explain the Code First Conventions in Entity Framework.
Code First Conventions are a set of rules that automatically configure the database schema based on the shape of your entity classes and their relationships.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 39. What is the purpose of the Fluent API in Entity Framework?
The Fluent API is an alternative to the Data Annotations for configuring the model in Entity Framework. It provides a more programmatic way to configure the database schema.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 40. Explain the purpose of the InverseProperty attribute in Entity Framework.
The InverseProperty attribute is used to specify the inverse navigation property in a relationship when the default convention is not followed.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 41. Explain the purpose of the Database.Log property in Entity Framework.
The Database.Log property is used to log SQL statements and other information related to database operations. It can be useful for debugging and performance tuning.
Example:
context.Database.Log = Console.WriteLine;
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 42. What is the purpose of the [ConcurrencyCheck] attribute in Entity Framework?
The [ConcurrencyCheck] attribute is used to specify that a property should be included in the WHERE clause of an UPDATE or DELETE statement to check for concurrency conflicts.
Example:
[ConcurrencyCheck] public byte[] RowVersion { get; set; }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 43. What is the purpose of the AsSplitQuery method in Entity Framework?
The AsSplitQuery method is used to enable split queries for a LINQ query, allowing EF to generate separate SQL queries for different parts of the LINQ query.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 44. Explain the purpose of the AsNoFilter method in Entity Framework.
The AsNoFilter method is used to disable query filters defined in the model, allowing you to retrieve entities without applying those filters.
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 45. What is the purpose of the Keyless attribute in Entity Framework?
The Keyless attribute is used to specify that an entity type does not have a primary key, and it is used for query types that represent database views or tables without primary keys.
Example:
[Keyless] public class MyQueryType { /* properties */ }
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Ques 46. What is the purpose of the Entry.StateChanged event in Entity Framework?
The Entry.StateChanged event is triggered when the state of an entity being tracked by the context changes. It can be used for implementing custom logic based on entity state changes.
Example:
entry.StateChanged += (sender, e) => { /* custom logic */ };
保存以便复习
保存以便复习
收藏此条目、标记为困难题,或将其加入复习集合。
Most helpful rated by users: