question

GuhananthS-8954 avatar image
0 Votes"
GuhananthS-8954 asked karenpayneoregon answered

Entity framework Enum fails

Hi
i have class file AppEnum.cs

public Enum Status
{
Approve =1 ,
Completed =2


}

in linq quer while joining 2 table how to use.
I have table called mapping table with colum status as integer, name as string,Id As Guid

Another table sortedFile table with columns Id As Guid,Name etc
private readonly IRepository<mappiing> mapping
private readonly IRepository<sortedFile> sorted
public class Mapper : IRepository<mappiing>, IRepository<sortedFile>
{


var str = from mapping.GetAll() innerjoin sort.GeAll()
where mapping.Status == App.Enum.Status --- fails here because App.Enum.Status is string. mapping.Status is interger
select sortedFile
{
Name = s. Name
}


}

dotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi GuhananthS-8954,
Enum support was introduced in Entity Framework 5. To use the new features like enums, spatial data types, and table-valued functions, you must target .NET Framework 4.5.
So please check your entity framework version.
Could you not cast it as an int?As follows:

 where mapping.Status ==(int)App.Enum.Status

Or try to change your Status enum to this:

 public Enum Status: : byte // Add byte here
 {
 Approve =1 ,
 Completed =2
 }

More details you can refer to this document.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

See my TechNet Wiki article and backing GitHub repository for working with enum with Entity Framework. Included in the GitHub repository is a T4 template for generating enums from a database.

Example

 public List<Product> GetBeveragesAndCondiments()
 {
   using (var context = new NorthWindContext())
   {
     return context
       .Products
       .Where(p => p.CategoryID == CategoriesName.Beverages ||
             p.CategoryID == CategoriesName.Condiments)
       .ToList();
   }
 }

Enum for above

 namespace NorthWindLibrary.Classes
 {
     public enum CategoriesName : int
     {
    
         /// <summary>
         /// Beverages 
         /// </summary>
         Beverages = 1,
    
         /// <summary>
         /// Condiments 
         /// </summary>
         Condiments = 2,
    
         /// <summary>
         /// Confections 
         /// </summary>
         Confections = 3,
    
         /// <summary>
         /// Dairy Products 
         /// </summary>
         DairyProducts = 4,
    
         /// <summary>
         /// Grains/Cereals 
         /// </summary>
         GrainsCereals = 5,
    
         /// <summary>
         /// Meat/Poultry 
         /// </summary>
         MeatPoultry = 6,
    
         /// <summary>
         /// Produce 
         /// </summary>
         Produce = 7,
    
         /// <summary>
         /// Seafood 
         /// </summary>
         Seafood = 8,
    
         /// <summary>
         /// Wine 
         /// </summary>
         Wine = 9
     }
 }




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.