LINQ to SQL InheritanceMappingAttribute property Code

3

What is the purpose of the Code property of the InheritanceMappingAttribute attribute?

The documentation describes this way:

  

This property holds a value that appears in the database table in the% col% of column to indicate which class or subclass this row of data belongs to.

I do not know what property this IsDiscriminator is. The documentation example is this:

public enum ShapeType
{
    Square = 0, Circle = 1
}
[Table(Name = "Shape")]
[InheritanceMapping(Code = ShapeType.Square, Type = typeof(Square),
    IsDefault = true)]
[InheritanceMapping(Code = ShapeType.Circle, Type = typeof(Circle))]

abstract public class Shape
{
    [Column(IsDiscriminator = true)]
    public ShapeType ShapeType = 0;
}

public class Square : Shape
{
    [Column]
    public int Side = 0;
}
public class Circle : Shape
{
    [Column]
    public int Radius = 0;
}

Is ShapeType the name of a column in the database table? What is the purpose of this column?

Here are the docs I'm reading doc1 doc2

    
asked by anonymous 22.04.2016 / 16:46

1 answer

0

It's all in the links you posted. The InheritanceMappingAttribute class maps an inheritance hierarchy to LINQ to SQL application.

The InheritanceMappingAttribute() constructor initializes a new instance of class InheritanceMappingAttribute .

Properties

  • Code : Gets or sets the value of the discriminator code in a mapped inheritance hierarchy.
  • IsDefault : Gets or sets if an object of this type is instantiated when the discriminator value does not match a specified value.
  • Type : Gets or sets the type of class in the hierarchy.
  • TypeId : When implemented in a derived class, gets a unique identifier for this Attribute . (inherited from Attribute.)

When mapping inheritance hierarchies, note the following:

  • All classes in a hierarchy should be mapped to a single table.

  • The table for an inheritance hierarchy must be declared in the mapped type that is at the top of the hierarchy. You can not specify table mapping attributes in a class that is derived from the top class.

  • You can use an interface in a hierarchy, but LINQ is not mapped.

  • You can ignore a class in the hierarchy when mapping classes, but you can only query mapped classes.

For the correct materialization, discriminator code values must be unique and match the values in the database. A line with a discriminator code value exactly matches (even by case) an instance of the class using IsDefault set to true .


On the IsDiscriminator property, it gets or sets whether a column contains a discriminator value for a LINQ to SQL inheritance hierarchy. The default is false .

When you use true , this property identifies the member of the class that contains the discriminator value for an inheritance hierarchy.

Sample Documentation :

[Column(Storage="_Title", DbType="NVarChar(30)",IsDiscriminator=true)]
public string Title
{
    get
    {
        return this._Title;
    }
    set
    {
        if ((this._Title != value))
        {
            this.OnTitleChanging(value);
            this.SendPropertyChanging();
            this._Title = value;
            this.SendPropertyChanged("Title");
            this.OnTitleChanged();
        }
    }
}

About the Shape class: see these two links from the documentation: Class Shape and ShapeType .

    
22.09.2016 / 17:32