Scottie
Scottie

Reputation: 11308

EF/Linq enum error

Given the following code:

public class RMAInfo
{
    public enum RMAStatuses {
        Undefined = 0, Approved = 1, Denied = 2,
        Pending = 3, Received = 4, Closed = 5
    }
    public enum ReturnLocations { Undefined = 0, Utah = 1, Indy = 2 }

    public RMAInfo()
    {
        ID = -1;
        RMACode = string.Empty;
    }

    public int ID { get; set; }
    public string RMACode { get; set; }
    public string ResellerID { get; set; }
    public RMAStatuses RMAStatus { get; set; }
}

private List<RMAInfo> GetRMAInfos_Internal(string resellerID)
{
    List<RMAInfo> returnRMAInfos = new List<RMAInfo>();

    using (Models.RMAEntities context = new Models.RMAEntities())
    {
        returnRMAInfos = (from r in context.RMAs
                          where r.ResellerID == resellerID
                          select new RMAInfo
                          {
                              ID = r.ID,
                              RMACode = r.RMACode,
                              ResellerID = r.ResellerID,
                              // error on next line!
                              RMAStatus = RMAInfo.RMAStatuses.Pending
                          }).ToList();
    }

    return returnRMAInfos;
}

I am getting an error on the assignment to the RMAStatus field. The error is

The specified value is not an instance of type 'Edm.Int32'

If I comment out that line, it works fine.

I have also tried to do this same code without using EF, and it seems to work fine.

Any ideas?

Upvotes: 4

Views: 3024

Answers (2)

Bradley Mountford
Bradley Mountford

Reputation: 8273

Installing .Net 4.5 appears to fix the issue as well (your project can still be on 4.0).

I was having this issue on our staging server (dev and test servers worked fine) and discovered that it did not have .Net 4.5 installed. Once I installed 4.5, the issue cleared up without any code changes.

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126834

Entity Framework does not like the enum, as it cannot translate it to SQL. You would need to expose a way for EF to set the underlying int value, or you would have to set the value yourself once EF was done with it.

What you might do is expose an int property to set it. If you wish, you could restrict it to internal access so that perhaps callers can't see it but your EF code can (assuming callers are in different assemblies, but your context is not). Then you could have

public class RMAInfo 
{
    ///<summary>
    /// Integer representation of RMAStatus
    ///</summary>
    internal int RMAStatusCode 
    { 
        get { return (int)this.RMAStatus; } // you could omit the getter
        set { this.RMAStatus = (RMAInfo.RMAStatuses)value; } 
    }
} 

...

select new RMAInfo 
{
     ...
     RMAStatusCode = (int)RMAInfo.RMAStatuses.Pending
}

To avoid this, you would basically select your RMAInfo sans status, and then iterate over the result to set each status to pending, leaving EF out of it entirely.

Upvotes: 4

Related Questions