# Monday, 09 January 2012

The most requested feature for Entity Framework has been Enum support for a very long time.  At TechEd this year it was announced that Enums would be supported in the next version of the Entity Framework and in June the Entity Framework June CTP was released with Enum support as well some other enhancements.

In the past I dealt with Entity Framework not supporting Enum by having an int property on my model that represented the Enum’s id.  My model also had a property of the Enum type that was marked as ignored so that EF did not try create a database column for the property.  The setter of the property would set the Enum id field and the getter would convert the Enum id field to the Enum type and return it.  If you are confused by that statement, like me, see the code below.

Model.cs
  1. public class Model {
  2.  
  3.         public long ModelId { get; set; }
  4.  
  5.         [Required]
  6.         [StringLength(50)]
  7.         public string Name { get; set; }
  8.  
  9.         [Required]
  10.         public int Year { get; set; }
  11.  
  12.         [Required]
  13.         public decimal BasePrice { get; set; }
  14.  
  15.         public int EngineLocation { get; set; }
  16.  
  17.         public EngineLocationType EngineLocationType {
  18.             get { return (EngineLocationType) EngineLocation; }
  19.             set { EngineLocation = (int) value; }
  20.         }
  21.  
  22.         #region << Navigation Properties >>
  23.  
  24.         public long ManufacturerId { get; set; }
  25.         public virtual Manufacturer Manufacturer { get; set; }
  26.  
  27.         public virtual IList<Engine> AvailableEngines { get; set; }
  28.  
  29.         #endregion
  30.  
  31.     }
DataContext.cs
  1. public class DataContext : DbContext {
  2.  
  3.         public DataContext() : base("name=EFCodeFirst") {}
  4.  
  5.         public IDbSet<Manufacturer> Manufacturers { get; set; }
  6.         public IDbSet<Model> Models { get; set; }
  7.         public IDbSet<Engine> Engines { get; set; }
  8.  
  9.         protected override void OnModelCreating(DbModelBuilder modelBuilder) {
  10.             modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  11.             modelBuilder.Entity<Model>().Ignore(x => x.EngineLocationType);
  12.             modelBuilder.Entity<Model>().HasMany(x => x.AvailableEngines).WithMany(x => x.AvailableOn).Map(x => x.MapLeftKey("ModelId").MapRightKey("EngineId").ToTable("ModelEngine"));
  13.             base.OnModelCreating(modelBuilder);
  14.         }
  15.     }

This gets the job done and really isn’t that messy, but with the new Enum support in the June CTP I can make things cleaner.  To make use of the new Enum support all I have to do is declare a property of EngineLocationType in my model and EF will make all the connections for me.

Model.cs
  1. public class Model {
  2.  
  3.         public long ModelId { get; set; }
  4.  
  5.         [Required]
  6.         [StringLength(50)]
  7.         public string Name { get; set; }
  8.  
  9.         [Required]
  10.         public int Year { get; set; }
  11.  
  12.         [Required]
  13.         public decimal BasePrice { get; set; }
  14.  
  15.         public EngineLocationType EngineLocation { get; set; }
  16.  
  17.         #region << Relationships >>
  18.  
  19.         public long ManufacturerId { get; set; }
  20.         public virtual Manufacturer Manufacturer { get; set; }
  21.  
  22.         public virtual IList<Engine> AvailableEngines { get; set; }
  23.  
  24.         #endregion
  25.  
  26.  
  27.     }

So that is the new EF Enum support.  The only issue I have with the implementation is that ideally I would like to be able to have an EngineLocationType table in my database that Entity Framework would create and populate based on my Enum definition and assign a foreign key relationship to EngineLocation on my Model entity.  That would make the entire process complete and architecturally sound IMHO.

If you are looking for a code download this is part of the Entity Framework Code First Demo that I did in a previous blog post and the code is available on GitHub.

Saturday, 28 January 2012 10:18:01 (Central Standard Time, UTC-06:00)
i enjoy your article. great job. keep it simple
delaco
Saturday, 22 September 2012 04:40:45 (Central Daylight Time, UTC-05:00)
Hi there I enjoy so very happier I looked for your new page, I actually discovered a person by the problem, because I had been looking into Aol available for something diffrent, Regardless Now I am suitable here at the present or would certainly love to state bless you to obtain marvelous posting and the over-all fascinating blog site (Also, I enjoy any theme/design), When I don’t have the time to successfully go through it's all at the moment but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great work.
Sunday, 30 September 2012 16:16:51 (Central Daylight Time, UTC-05:00)
i enjoy your article. great job. keep it simple
Name
E-mail
(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