Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a EF 4.1 Code First tutorial the following code is given: public class Depart

ID: 643262 • Letter: I

Question

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.)

Explanation / Answer

Data annotations are static, for instance this method declaration cannot change at runtime:

[MinLength(5)]
[MaxLength(20,ErrorMessage="Le nom ne peut pas avoir plus de 20 caract

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote