There is an DataAnnotation attribute counterpart for the HasOptional method of the code-first fluent API?
I want to mark my property with attribute instead of using the fluent API.
This is my current model & OnModelCreating code:
public class Employee
{
#region Properties
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int? ManagerID { get; set; }
public Employee Manager { get; set; }
#endregion
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().
HasOptional(e => e.Manager).
WithMany().
HasForeignKey(m => m.ManagerID);
}
Thanks
No, such an attribute doesn’t exist (in contrast to the
[Required]attribute). I think the reason is that it is not necessary because[Required]attribute would be necessary to make the relationship required.So, there doesn’t seem to be a need for such an attribute (unless perhaps to make the optional relationship explicit in the class definition, but a comment above the property would do the same).