Entity Framework Interview Questions and Answers
Ques 16. 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 17. 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 18. 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 19. 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 20. 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 */ }
Most helpful rated by users: