# Monday, 05 December 2011

Configuration of Entity Framework Code First is a necessary evil and you have to admit is a very well designed user friendly process.  But, in typical Microsoft fashion, the EF developers have given us more than one way to perform this configuration.

  1. Fluent API
    1. Fluent expression based configuration
    2. Performed during the OnModelCreating process of the DbContext.
    3. Allows for complex configurations that are not possible via Attribute based configuration
      Fluent Configuration
      1. protected override void OnModelCreating(DbModelBuilder modelBuilder) {
      2.     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
      3.     modelBuilder.Entity<Model>().HasMany(x => x.AvailableEngines)
      4.         .WithMany(x => x.AvailableOn)
      5.         .Map(x => x.MapLeftKey("ModelId")
      6.         .MapRightKey("EngineId")
      7.         .ToTable("ModelEngine"));
      8.     modelBuilder.Entity<Model>().Ignore(x => x.SomeDerivedProperty);
      9.     base.OnModelCreating(modelBuilder);
      10. }

  2. Attributes
    1. Attribute based configuration
    2. Performed directly on the Domain Model Class/Property being configured
    3. Allows the non-EF specific attributes to be used in other pieces of your application
    4. Allows validation of your Model at any time
      Attribute Configuration
      1. public class Manufacturer {
      3.     public long ManufacturerId { get; set; }
      5.     [StringLength(40)]
      6.     [Required]
      7.     public string Name { get; set; }
      9.     [Required]
      10.     [StringLength(40)]
      11.     public string Country { get; set; }
      13.     [Required]
      14.     public DbGeography Location { get; set; }
      16.     #region << Relationships >>
      18.     public virtual IList<Model> Models { get; set; }
      20.     #endregion
      22. }


So…When do you use which!?

Use Fluent Configuration When:

  • Configuration is being done solely to make the database behave correctly.
  • Configuration has no effect on the design and/or behavior of your Domain Model.
  • Examples:
    • Specifying the database table the Domain Model is to be mapped to
    • Specifying the database table column the Domain Model Field is to be mapped to
    • Ignoring a Domain Model Field
    • Configuring the keys and table names for relationships not able to be picked up by EF

Use Attribute Configuration When:

  • Configuration solely applies to your Domain Model.
  • Configuration has no effect on the design and/or behavior of your Database.
  • Examples:
    • Making a field Required
    • Validating a field (String length, Range, etc.)
  • Also…Don’t forget that for the most part these Attributes are not EF specific and can be used in the UI with MVC.
Monday, 30 January 2012 07:09:28 (Central Standard Time, UTC-06:00)
I enjoyed reading this blog article. thanks for the information. obat kuat foredi
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview