Entity framework mapping enum : The specified value is not an instance of type 'Edm.Int32' Parameter name: value
Asked Answered
Y

3

9

I'm trying to return the result of a entity framework query into my own dto class, at the same time as mapping my enum TradeType.

I'm getting the following error

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

Any idea how to fix or a workaround?

Thanks

public IEnumerable<Trade> GetLiveTrades(string routeName)
{
    return _entities.Price.Where(p => p.StatusCode.Equals("A") && p.Period.PeriodYear <= DateTime.Now.Year+1 && p.Route.RouteCode.Equals(routeName)).
        Select(p => new Trade
                        {
                            Volume = (long) (p.Volume ?? 100), 
                            TradeType = (p.PriceTypeCode.Equals("O") ? TradeType.Seller : TradeType.Bidder),
                            Price = p.Price1,
                            TenorStartDate = p.Period.PeriodStartDate.Value,
                            TenorEndDate = p.Period.PeriodStartDate.Value,
                            TradeId = p.ID
                        }).ToList();
        }

public class Trade
{
    public long Volume { get; set; }
    public TradeType TradeType { get; set; }
    public double Price { get; set; }
    public DateTime TenorStartDate { get; set; }
    public DateTime TenorEndDate { get; set; }
    public Guid TradeId { get; set; }
}
Yance answered 3/4, 2012 at 9:6 Comment(0)
D
10

Enums in projections from Entity Framework projections (Select) is a know issue. If you do

_entities.Price.Where(p => p.StatusCode.Equals("A") &&
    p.Period.PeriodYear <= DateTime.Now.Year + 1 &&
    p.Route.RouteCode.Equals(routeName)).ToList() // ToList !
    .Select(p => new Trade ...

the projection is done by regular linq-to-objects, which is a routine job.


EDIT

As a late afterthought I'd like to add that just a dumb ToList() can be detrimental when the table in question has many columns. It would mean that far more data is transferred to the client than necessary. In such cases it can be useful to do a double projection. First, within the scope of the query provider project to accepted types. Then, after switching to linq-to-objects (AsEnumerable) project to the final type:

_entities.Price.Where(p => p.StatusCode.Equals("A") &&
    p.Period.PeriodYear <= DateTime.Now.Year + 1 &&
    p.Route.RouteCode.Equals(routeName))
    .Select(p => new 
                {
                  Volume = (long) (p.Volume ?? 100), 
                  PriceTypeCode = p.PriceTypeCode,
                  Price = p.Price1,
                  TenorStartDate = p.Period.PeriodStartDate.Value,
                  TenorEndDate = p.Period.PeriodStartDate.Value,
                  TradeId = p.ID
                })
    .AsEnumerable()
    .Select(x => new Trade
                 {
                   Volume = x.Volume, 
                   TradeType = (x.PriceTypeCode.Equals("O") ? TradeType.Seller : TradeType.Bidder),
                   Price = x.Price1,
                   TenorStartDate = x.TenorStartDate,
                   TenorEndDate = x.TenorEndDate,
                   TradeId = x.ID
                 }).
Damocles answered 3/4, 2012 at 21:34 Comment(4)
Thanks, I got around the problem by changing my dto to have an internal varible which stored the original text version of PriceTypeCode then I exposed a public property that performs a switch to return the correct enum. A little bit hacky but it delivered the desired results! :). Cheers for your answers, I'll use your method going forward.Yance
Thanx this helped me solve my issue, I changed my code to ToString() the enum into a local variable outside the query and that solved it for me.Cesspool
Hi @John. Your comment drew my attention to this post again and urged me to append an afterthought.Damocles
So then AsEnumerable allows you to use linq to objects without executing to the database yet ? thats coolCesspool
T
9

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.

Tully answered 3/9, 2013 at 14:32 Comment(4)
This is right and as times goes by this will become the answer to this question.Damocles
How does this resolve the issue on Windows XP machines?Hightoned
@BitOriented Are you arguing that a bug fix in the new version of .Net is a less preferable solution to rolling your own workarounds because an old, unsupported, non-server environment cannot implement it?Tully
UGH this bug wasn't occurring on my local machine and was on a server. In my environment (all depends on your environment) I was able to quickly make the code workaround and deploy a new DLL to the DEV server where this error was occurring, which is faster than asking someone else to update the .NET framework of this DEV server which I otherwise don't control. Yay!Fatness
B
0

I got this error when trying to update entities, I'm adding this answer because this question is the top result when searching for that eerror. For updating, it is due to a bug in MySQL Connector:

https://bugs.mysql.com/bug.php?id=44801

It appears to affect version 6.0.3 and below:

See the release notes for 6.0.3 MySQL Connector for .Net

It informs that the unsigned int are not supported for EF int this version.

The easiest fix (for me) was to change the column to be signed instead of unsigned.

Blinnie answered 3/1, 2019 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.