LINQ to SQL FAQ – Mapping Inheritance in LINQ

Mapping table entities to different types

By default LINQ-to-SQL will map each table to a single class, usually the singular name of the table. So for example the Employees table in NorthWind maps to an Employee class.

However, you may well be creating subclasses to deal with different types of order, for example, and you don’t want a plain old ‘Employee; object returned, you really want the subclass. This is supported in LINQ-to-SQL using the InheritanceMapping Attribute and the  IsDiscriminator setting on a column. It allows you to specify one field of the database table as a selector, and then return different classes based on the value of the selector.

I won’t go into a long example of this since David Hayden’s blog has a really nice one and it would be a waste of bandwidth to just rehash this.

For more info see the Microsoft web article on MSDN.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s