Entity query throws "Invalid operation exception" in enum field

I was just trying to use some features of the framework 5 and kendo ui entity. I have the following ProductType enum

public enum ProductType {
    [Description("Hazardous")]
    Hazardous,
    [Description("Non Hazardous")]
    NonHazardous
}

This type of listing is one of the fields in the Product object .

[Table("Products")]
public class Product {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    //Other Fields ...

    [Required(ErrorMessage="Product Type is Required")]
    public ProductType ProductType {get;set;}

}

The ProductType column in the SQL Server base database is defined as (tinyint, not null) .

I access the product list in the next MVC controller action

    public ActionResult _ProductList(int pageSize, int skip) {
        using (WorkUnit workUnit = new WorkUnit()) {
            IQueryable<Product> products = workUnit.ProductRepository.GetAllProducts()
                                                   .Include(p => p.Category);
            int total = products.Count();
            List<Product> productList = products.ToList<Product>(); //Throws InvalidOperationException
            return Json(new { total = total, data = productList }, JsonRequestBehavior.AllowGet);
        }
    }

Here is a description of the InvalidOperationException that is thrown in products.ToList () in the above controller action.

The 'ProductType' property on 'Product' could not be set to a 'Byte' value. 
You must set this property to a non-null value of type 'ProductType'.

Any guesses why ToList () throws an InvalidOperationException .

, , POCO.

Edit

  System.InvalidOperationException was unhandled by user code
  HResult=-2146233079
  Message=The 'ProductType' property on 'Product' could not be set to a 'Byte' value. You must set this property to a non-null value of type 'ProductType'. 
  Source=System.Data.Entity
  StackTrace:
       at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
       at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
       at lambda_method(Closure , Shaper )
       at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
       at lambda_method(Closure , Shaper )
       at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
       at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at KendoUI_Web_Demo.Controllers.HomeController._ProductList(Int32 pageSize, Int32 skip) in d:\Practice\ASP.NET_MVC\KendoUI_Web_Demo\KendoUI_Web_Demo\Controllers\HomeController.cs:line 52
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
       at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
  InnerException: 
+4
1

, int, , (tinyint) . , :

public enum ProductType : byte
{
...
}
+15

Source: https://habr.com/ru/post/1525418/


All Articles