title: Error message will using enum with EF and Web API
body: <p>I'm working on a .NET project with EF 6.0.</p>
  
  <p>** Please note that the database already exist and cannot be changed by code **</p>
  
  <p>I'm using enum and here's my code :</p>
  
  <pre><code>[Table("T_PRODUCTS")]
  public class basket
  {
      [Key]
      public int productID{get;set;}
      public string productName {get;set;}
      public EProductType productType {get;set;}
  }
  
  public enum EProductType
  {
      typeA = 0,
      typeB = 1,
  }
  </code></pre>
  
  <p>And the definition of my table in SQL Server :</p>
  
  <pre><code>CREATE TABLE [dbo].[T_PRODUCTS](
      [productID] [int] NOT NULL,
      [productName] [varchar](100) NULL,
      [productType] [smallint] NULL,
  CONSTRAINT [PK_T_PRODUCTS] PRIMARY KEY CLUSTERED 
  (
      [productID] ASC
  )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  ) ON [PRIMARY]  
  </code></pre>
  
  <p>While running unit tests, data are saved successfully in the database! ;)</p>
  
  <p>however, when I use my web api :</p>
  
  <pre><code>public class ProductController : ApiController
  {
      public HttpResponseMessage Get( int id )
      {
          var Product=  Products.GetById( id );
  
          if ( Product == null )
          {
              return Request.CreateResponse( HttpStatusCode.NotFound );
          }
  
          return Request.CreateResponse( HttpStatusCode.OK, Product );
      }       
  }
  </code></pre>
  
  <p>I have this error message :</p>
  
  <pre><code>&lt;Error&gt;&lt;Message&gt;An error as occured.&lt;/Message&gt;&lt;ExceptionMessage&gt;Property 'productType' on 'Product' could not be set to 'System.Int16'. You must assign a non null value of type 'EProductType' to this property. &lt;/ExceptionMessage&gt;
  &lt;ExceptionType&gt;System.InvalidOperationException&lt;/ExceptionType&gt;
  </code></pre>
  
  <p>If I change my enum like this :</p>
  
  <pre><code>public enum EProductType : ushort
  {
      typeA = 0,
      typeB = 1,
  }
  </code></pre>
  
  <p>It will return my product without error.</p>
  
  <p>But, the field 'productType' is always set to 0 (zero). 
  And then, all new product saved with EF have NULL in the field 'productType'...</p>
  
  <p>How can I use enums with EF and a Web api ?</p>
  
  <p>Thanks for your replies.</p>