Skip to content

Data.Linq.Inheritance

Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Data / Linq

BLToolkit inheritance support is similar to Linq To SQL inheritance. The following example shows how to define inheritance for the Product table:

[TableName("Products")]
[InheritanceMapping(Code="True",  Type=typeof(DiscontinuedProduct))]
[InheritanceMapping(Code="False", Type=typeof(ActiveProduct))]
public abstract class Product
{
    [PrimaryKey, Identity]                      public int      ProductID;
    [NotNull]                                   public string   ProductName;
                                                public int?     SupplierID;
                                                public int?     CategoryID;
                                                public string   QuantityPerUnit;
                                                public decimal? UnitPrice;
                                                public short?   UnitsInStock;
                                                public short?   UnitsOnOrder;
                                                public short?   ReorderLevel;
    [MapField(IsInheritanceDiscriminator=true)] public bool     Discontinued;
}

public class ActiveProduct : Product
{
}

public class DiscontinuedProduct : Product
{
}

The Discontinued field is a discriminator that defines the actual object type. See Linq To SQL Inheritance for more details.

Example:

from p in db.DiscontinuedProduct
select p;

SQL:

SELECT
    [p].[ProductID],
    [p].[ProductName],
    [p].[SupplierID],
    [p].[CategoryID],
    [p].[QuantityPerUnit],
    [p].[UnitPrice],
    [p].[UnitsInStock],
    [p].[UnitsOnOrder],
    [p].[ReorderLevel],
    [p].[Discontinued]
FROM
    [Products] [p]
WHERE
    [p].[Discontinued] = 'True'

The same SQL will be generated for the following:

from c in db.Product
where c is Northwind.DiscontinuedProduct
select c;
Clone this wiki locally