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 不會對應它。

  • 當您對應類別時,您可以略過階層中的類別,但只能查詢對應的類別。

若要進行正確的具體化,鑒別子程式代碼值必須是唯一的,且符合資料庫中的值。 即使大小寫) 將 IsDefault 類別 true具現化為,具有不完全相符 (的鑒別子程式代碼值數據列。

建構函式

InheritanceMappingAttribute()

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

屬性

Code

取得或設定對應之繼承階層架構中的鑑別子碼值。

IsDefault

取得或設定鑑別子值不符合指定的值時,這個型別的物件是否會執行個體化。

Type

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

TypeId

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

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

_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)

適用於