views:

456

answers:

1

What I have:

LINQ to SQL auto generated class, which corresponds to the shape of its SQL table.

TABLEA
========
column1
column2

What I want to do:

Extend TABLEA class to include a new property, whose related column will be present only when a particular stored procedure is called. The stored procedure return type will be of TABLEA.

TABLEA
========
column1
column2
newcolumn3 (always part of the class structure, not always populated)   

What I've done so far:

Created a partial class with a property and a columnattribute, which works when the stored procedure is called. When a direct table call is made, I get an invalid column exception.

My question is:

How do I extend my LINQ to SQL data class to populate a property from a column which may not be present?

+2  A: 

I don't think that is possible. The point of LINQ to SQL is to provide a direct mapping between a return set schema and an object in .NET. Two different return set schemas, two different objects. You can't truly think of the stored procedure as returning the same object if the signature of the return is different. It's more like "ObjectWithAdditionalInfo".

When you start to do these more complex, ambiguous mappings you need a real ORM tool like Microsoft Entities or Hibernate.

Rex M
Well said, LINQ is not a crystal ball, it only knows about the current schema.
Chris Ballance
Thanks. While there are several ways to skin this cat, I'm exploring my options and learning LINQ at the same time. @Chris -- I don't need it to be a crystal ball; just not throw an exception when a mapped column isn't there, or at least provide a different way to populate my partial properties.
JazzHands