Entity Framework 面试题与答案
问题 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; }
问题 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.
问题 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");
问题 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;
问题 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 */ }
用户评价最有帮助的内容: