InheritanceMappingAttribute 類別

定義

對應 LINQ to SQL 應用程式中的繼承階層。

public ref class InheritanceMappingAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=true, Inherited=false)]
public sealed class InheritanceMappingAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=true, Inherited=false)>]
type InheritanceMappingAttribute = class
    inherit Attribute
Public NotInheritable Class InheritanceMappingAttribute
Inherits Attribute
繼承
InheritanceMappingAttribute
屬性

範例

此範例展示了階層結構中頂端映射類別(Shape)為抽象的繼承映射。

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;
}
Public Enum ShapeType As Integer
    Square = 0
    Circle = 1
End Enum

<Table(Name:="Shape")> _
<InheritanceMapping(Code:=ShapeType.Square, Type:=GetType(Square), _
    IsDefault:=True)> _
<InheritanceMapping(Code:=ShapeType.Circle, Type:=GetType(Circle))> _
Public MustInherit Class Shape
    <Column(IsDiscriminator:=True)> _
        Public ShapeType As ShapeType = 0
End Class

Public Class Square
    Inherits Shape
    <Column()> _
    Public Side As Integer = 0
End Class

Public Class Circle
    Inherits Shape
    <Column()> _
    Public Radius As Integer = 0
End Class

以下範例展示了未映射類別的包含。 你可以把未映射的類別放在階層結構的任何位置。

// Unmapped and not queryable.
class A {  }

// Mapped and queryable.
[Table]
[InheritanceMapping(Code = "B", Type = typeof(B),
IsDefault = true)]
[InheritanceMapping(Code = "D", Type = typeof(D))]
class B: A {  }

// Unmapped and not queryable.
class C: B {  }

// Mapped and queryable.
class D: C {  }

// Unmapped and not queryable.
class E: D {  }
' Unmapped and not queryable.
Class A
End Class

' Mapped and queryable.
<Table()> _
<InheritanceMapping(Code:="B", Type:=GetType(B), _
IsDefault:=True)> _
<InheritanceMapping(Code:="D", Type:=GetType(D))> _
Class B
    Inherits A
End Class

' Unmapped and not queryable.
Class C
    Inherits B
End Class

' Mapped and queryable.
Class D
    Inherits C
End Class

' Unmapped and not queryable.
Class E
    Inherits D
End Class

備註

每個映射類別會指定一個 InheritanceMappingAttribute

當你繪製繼承階層時,請注意以下幾點:

  • 階層中的所有類別必須映射到單一資料表。

  • 繼承階層的表格必須在階層頂端的映射型別上宣告。 你無法在從頂層類別衍生出的類別中指定表格或映射屬性。

  • 你可以在階層結構中使用介面,但 LINQ 不會映射它。

  • 你可以在映射類別時跳過階層中的一個類別,但只能查詢映射過的類別。

為了正確的物質化,判別碼值必須是唯一且與資料庫中的值相符的。 若一列判別子代碼值不完全匹配(即使大小寫)則透過 set 到 true來實例化該類別IsDefault

建構函式

名稱 Description
InheritanceMappingAttribute()

初始化 InheritanceMappingAttribute 類別的新執行個體。

屬性

名稱 Description
Code

取得或設定判別器程式碼值,並設定在映射的繼承階層中。

IsDefault

當判別子值與指定值不符時,是否實例化該類型物件。

Type

取得或設定階層中類別的類型。

TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於