LINQ to SQL Inheritancemappingattribute Code property

Asked

Viewed 96 times

3

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

The documentation describes 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 that property is IsDiscriminator. The example of the documentation 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 the Docs I’m reading doc1 doc2

  • Taking advantage of the topic, I would like to ask another question. Will the data contained in the three classes be saved in the same table in the database? What if in the database they are in separate tables? For example, say I have, the following hierarchy Veiculo(Father), Carro(Derivative), Moto(Derived) and that in db they are represented in separate tables, ie a table p/ each class, simulating inheritance through relationship. How it would look?

  • Cont.... . I mean, what would the code c# LINQ look like, and not the structure of the tables. The tables I already have ready.

1 answer

0

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

The builder InheritanceMappingAttribute() initializes a new class instance InheritanceMappingAttribute.

Estates

  • Code: Obtains or defines the value of the code discriminator in a mapped heritage hierarchy.
  • IsDefault: Obtains or defines whether an object of this type at the instantiation when the value of the discriminator does not match a specified value.
  • Type: Obtains or defines the class type in the hierarchy.
  • TypeId: When implemented in a derivative class, you get 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 cannot specify table mapping attributes in a class that is derived from the upper 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 refer to mapped classes.

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


On the property IsDiscriminator, it obtains or defines 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 class member that contains the value of the discriminator for an inheritance hierarchy.

Example of 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 in the documentation: Class Shape and Shapetype.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.