c# – LINQ to SQL InheritanceMappingAttribute propriedade Code

Question:

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

The documentation describes it this way:

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.

I don't know what property this IsDiscriminator . 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;
}

ShapeType is 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

Answer:

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

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

properties

  • Code : Gets or sets the value of the code discriminator in a mapped inheritance hierarchy.
  • IsDefault : Gets or sets whether an object of this type is not instantiated when the discriminator value does not match a specified value.
  • Type : Gets or sets the type of the 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 must be mapped to a single table.

  • The table for an inheritance hierarchy must be declared on the mapped type that is at the top of the hierarchy. You cannot specify table mapping attributes on 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 correct materialization, discriminator code values ​​must be unique and match values ​​in the database. A line with a discriminator code value matches exactly (even by case) creates 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.

Documentation example:

[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 documentation links: Class Shape and ShapeType .

Scroll to Top