Home Dashboard Directory Help

LINQ to SQL: Bug Handling Entities with Common Base Class by CompuBoy


Status: 

Closed
 as Won't Fix Help for as Won't Fix


2
0
Sign in
to vote
Type: Bug
ID: 394255
Opened: 1/10/2009 12:58:53 AM
Access Restriction: Public
1
Workaround(s)
view
1
User(s) can reproduce this bug

Description

I would like to make a base class for my LINQ to SQL entities to enforce having ID as PK. I have written something like this:

public abstract class Entity
{
    public virtual long ID { get; set; }
}


Now, Consider the following LINQ to SQL model:

[Table]
public partial class Order : Entity
{
     [Column(Name="OrderID", IsPrimaryKey = true)]
     public override long ID { get; set; }
}

Now, If you run the following query:

var db = new DataContext("someconnectionstring");
var orders = db.Orders.Where(o => o.ID == 10).ToList();

You will get an InvalidOperationException with message "Class member Entity.ID is unmapped.", but if I change "ToList" to "Single" or "SingleOrDefault", or I use a more complex predicate, or I query my entity using a field other than PK, no exception is thrown.

Analyzing the scenario more thoroughly, I found that the MemberExpression generated for o.ID references "Entity.ID" instead of "Order.ID". I'm not sure whether this is the correct way of constructing the expression for the predicate I have given.

And it seems that LINQ to SQL, does not handle this way expression construction correctly in the special case of querying the entity via PK and materializing it as a list. BTW, if I write my query like:

var orders = db.Orders.Where(o => ((Order)o).ID == 10).ToList();

It works as it should without any exceptions. Even writing something like this won't throw any exceptions:

var orders = db.Orders.Select(o => o).Where(o => ((Order)o).ID == 10).ToList();

The stack trace of the exception shows that it is somehow related with looking up the requested entity in the identity cache.





Details
Sign in to post a comment.
Posted by Microsoft on 2/25/2009 at 4:42 PM
Hi,

Thank you for taking the time to send this feedback and bug report. We have reviewed the issue and confirmed the behavior, but we will not be fixing this in the next release of LINQ to SQL.

LINQ to SQL Team
Posted by blepeuple on 2/16/2009 at 7:29 AM
Just tested: using an external xml mapping seems to be a viable workaround.

Posted by blepeuple on 2/16/2009 at 7:10 AM
I am hitting the same bug here.

The simple statement
Order order = db.Orders.Single(o => o.ID == 10);

throws the exception.

I just wanted to stress out two points
1) It seems like a very common scenario
2) It is, I think, a critical issue

Regards
Posted by Microsoft on 1/13/2009 at 12:36 AM
We were able to reproduce the issue you are seeing. We are escalating this issue to the product unit who works on that specific feature area. The product team will review this issue and make a decision on whether they will fix it or not for the next release.

Posted by Microsoft on 1/12/2009 at 3:13 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by Joseph Gordon on 4/15/2010 at 12:42 PM
A workaround that worked for me: Change this line:

var orders = db.Orders.Where(o => o.ID == 10).ToList();

to this:

var orders = db.Orders.Where(o => ((Order)o).ID == 10).ToList();