开发者

EF4 - Linq - Exception on Query.Any()

开发者 https://www.devze.com 2023-03-02 09:46 出处:网络
I\'ve using EF for a while but I\'ve never had this problem. Basically we have a WCF services that provide data for web front end. In that service, we use EF 4 as data implementation. Bypass all the r

I've using EF for a while but I've never had this problem. Basically we have a WCF services that provide data for web front end. In that service, we use EF 4 as data implementation. Bypass all the repository and singleton, a simple Get function would be as below:

using (OurEntities DataContext = new OurEntities())
{
    DataContext.Order.MergeOption = System.Data.Objects.MergeOption.NoTracking;
    List<Order> orders = new List<Order>();
    var query = from p in DataContext.Order.Include("OrderDetail")
                where (p.OrderID == orderId || orderId == 0)
                   && (p.OrderStatus == orderStatus || orderStatus == 0)
                   && (p.OrderType == orderType || orderType == 0)
                   && (p.OrderFlag == null || p.OrderFlag == false)
                select p;

    if (query.Any())
    {
        foreach (Order order in query)
        {
            orders.Add(order);
        }
    }
    return orders;
}

orderId, orderStatus & orderType are passed in parameters.

The code works without any problems, as expected... until we run a few stress tests where we call the services (i.e. the GET functions) simultaneously from different clients. After a few minutes, we get a bunch of InvalidOperationExcept开发者_运维知识库ion: The specified cast from a materialized 'System.Int32' type to the 'System.Boolean' type is not valid. In our SQL 2008 database, OrderID is int (identity, auto-gen) and OrderFlag is the only field with data type = bit (translated to boolean by EF).

During debug, I found out that the exception was either thrown by query.Any() or by the foreach clause, when a single item in query is casted to Order. But if I touch the transaction by any mean (either run the same query on SSMS or execute query.Any() in the Watch window), query get updated with proper data and it just works....

Our evironment: VS 2010, .Net framework 4, EF 4, SQL Server 2008 express + standard (I've tried on both)

Any comments or any help would be greatly appreciated...

Eric


You should start with modifying your code to:

using (OurEntities DataContext = new OurEntities())
{
    DataContext.Order.MergeOption = System.Data.Objects.MergeOption.NoTracking;
    var query = from p in DataContext.Order.Include("OrderDetail")
                where (p.OrderID == orderId || orderId == 0)
                   && (p.OrderStatus == orderStatus || orderStatus == 0)
                   && (p.OrderType == orderType || orderType == 0)
                   && (p.OrderFlag == null || p.OrderFlag == false)
                select p;

    return query.ToList();
}

And run your tests again. I don't except that this will solve your problem but your current code is something that should not pass code review.

0

精彩评论

暂无评论...
验证码 换一张
取 消