How to: Map Inheritance Hierarchies (LINQ to SQL)
To implement inheritance mapping in LINQ, you must specify the attributes and attribute properties on the root class of the inheritance hierarchy as described in the following steps. Developers using Visual Studio can use the Object Relational Designer to map inheritance hierarchies. How to: Configure Inheritance by Using the O/R Designer
How to: Configure Inheritance by Using the O/R Designer
No special attributes or properties are required on the subclasses. Note especially that subclasses do not have the TableAttribute attribute.
To map an inheritance hierarchy
Add the TableAttribute attribute to the root class.
Also to the root class, add an InheritanceMappingAttribute attribute for each class in the hierarchy structure.
This property holds a value that appears in the database table in the IsDiscriminator column to indicate which class or subclass this row of data belongs to.
This property holds a value that specifies which class or subclass the key value signifies.
This property serves to designate a fallback mapping when the discriminator value from the database table does not match any Code value in the inheritance mappings.
This property signifies that this is the column that holds the Code value.
If you are using Visual Studio, you can use the Object Relational Designer to configure inheritance. How to: Configure Inheritance by Using the O/R Designer
In the following code example, Vehicle is defined as the root class, and the previous steps have been implemented to describe the hierarchy for LINQ. The following illustration shows the relationships.
<Table()> _ <InheritanceMapping(Code:="C", Type:=GetType(Car))> _ <InheritanceMapping(Code:="T", Type:=GetType(Truck))> _ <InheritanceMapping(Code:="V", Type:=GetType(Vehicle), _ IsDefault:=True)> _ Public Class Vehicle <Column(IsDiscriminator:=True)> _ Private DiscKey As String <Column(IsPrimaryKey:=True)> _ Private VIN As String <Column()> _ Private MfgPlant As String End Class Public Class Car Inherits Vehicle <Column()> _ Private TrimCode As Integer <Column()> _ Private ModelName As String End Class Public Class Truck Inherits Vehicle <Column()> _ Private Tonnage As Integer <Column()> _ Private Axles As Integer End Class