Entity Framework Interview Questions and Answers
Ques 31. 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 32. 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 33. 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 34. 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 35. 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);
Most helpful rated by users: