title: Many-to-Many Relationship Between Enum and Entity is Not Being Stored
body: <p>I am working on a Entity Framework Code First project where I have many-to-many relationships:</p>
  
  <p>Service Providers can have many Service Types, and Service Types can have many Service Providers. </p>
  
  <p>Services have only one Service Type.</p>
  
  <p><strong>Service Type</strong> is an enum:</p>
  
  <pre><code>public enum ServiceTypeEnum
  {
      Ambulance = 1,
      [Display(Name = "Cash Advance")]
      CashAdvance = 2,
      Hospitalization = 3,
      Hotel = 4,
      [Display(Name = "House Call")]
      HouseCall = 5,
      [Display(Name = "Medical Escort")]
      MedicalEscort = 6,
      Transfer = 7,
      Repatriation = 8
  }
  </code></pre>
  
  <p><strong>Service Provider</strong> </p>
  
  <pre><code>public partial class ServiceProvider
  {
      public ServiceProvider()
      {
          ServiceTypes = new HashSet&lt;ServiceTypeEnum&gt;();
      }
  
      [Key]
      public int ServiceProviderID { get; set; }
  
      [Required]
      [StringLength(100)]
      public string Title { get; set; }
  
      // This is OK for a single Service 
      //public virtual ServiceTypeEnum ServiceType { get; set; }
  
      // I added this so that Service Providers can have multiple Service Types
      public ICollection&lt;ServiceTypeEnum&gt; ServiceTypes { get; set; }
  
      public IEnumerable&lt;Service&gt; Services { get; set; } 
  }
  </code></pre>
  
  <p>and <strong>ServiceProviderViewModel</strong>: </p>
  
  <pre><code>public class ServiceProviderViewModel
  {
      public class CreateModel
      {
          public int ServiceProviderID { get; set; }
  
          [Required]
          [StringLength(100)]
          [Display(Name = "Title")]
          public string Title { get; set; }
  
          public IEnumerable&lt;KeyValuePair&lt;string, int&gt;&gt; AllServiceTypes { get; set; }
          public string[] SelectedServiceTypes { get; set; }
  
          //public ServiceTypeEnum ServiceType { get; set; }
      }
  
      public class EditModel
      {
          ...
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and finally <strong>ServiceProviderController</strong>:</p>
  
  <pre><code> public ActionResult Create()
      {
          var _allServiceTypes =  Enum.GetValues(typeof(ServiceTypeEnum))
             .Cast&lt;ServiceTypeEnum&gt;()
             .Select(t =&gt; new KeyValuePair&lt;string, int&gt;(t.ToString(), (int) t));
  
          var viewModel = new ServiceProviderViewModel.CreateModel()
          {
              AllServiceTypes = _allServiceTypes
          };
  
          return View(viewModel);
      }
  
      [HttpPost]
      public ActionResult Create(ServiceProviderViewModel.CreateModel viewModel)
      {
          if (ModelState.IsValid)
          {
              // This is OK for a single Service Type
              //var serviceProvider = new ServiceProvider
              //{
              //    Title = viewModel.Title,
              //    ServiceType = viewModel.ServiceType
              //};
  
              // For multiple Service Types
              var serviceProvider = new ServiceProvider();
              serviceProvider.Title = viewModel.Title;
              for (int i = 0; i &lt; viewModel.SelectedServiceTypes.Length; i++)
              {
                  serviceProvider.ServiceTypes.Add((ServiceTypeEnum)Enum.Parse(typeof(ServiceTypeEnum), viewModel.SelectedServiceTypes[i].ToString()));
              }
  
              repository.InsertServiceProvider(serviceProvider);
  
  
              repository.Save();
              return RedirectToAction("Index");
          }
          return View(viewModel);
      }
  </code></pre>
  
  <p>The problem is; there are no tables or columns in the database for the added service types. While debugging, I can see that the selected values of checkboxes are converted to respective enums and are added to serviceProvider.ServiceTypes</p>
  
  <p>There's nothing when I try to get the service types:</p>
  
  <pre><code>repository.ServiceProviders.ServiceTypes
  </code></pre>
  
  <p>What am I missing?</p>