In a EF 4.1 Code First tutorial the following code is given:
public class Department
{
public int DepartmentId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Collaborator> Collaborators { get; set; }
}
Then it is explained that the fluent interface is more flexible:
Data Annotations are definitely easy to use but it is preferable to use a programmatic approach that provides much more flexibility.
The example of using the fluent interface is then given:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>().Property(dp => dp.Name).IsRequired();
modelBuilder.Entity<Manager>().HasKey(ma => ma.ManagerCode);
modelBuilder.Entity<Manager>().Property(ma => ma.Name)
.IsConcurrencyToken(true)
.IsVariableLength()
.HasMaxLength(20);
}
I can't understand why the fluent interface is supposedly better. Is it really? From my perspective it looks like the data annotations are more clear, and have more of a clean semantic feel to it.
My question is why would a fluent interface be a better option than using attributes, especially in this case?
(Note: I'm quite new to the whole concept of fluent interfaces, so please expect no prior knowledge on this.)
Reference: http://codefirst.codeplex.com/