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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于