MetadataTypeAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定要与数据模型类关联的元数据类。
public ref class MetadataTypeAttribute sealed : Attribute[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)]
public sealed class MetadataTypeAttribute : Attribute[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public sealed class MetadataTypeAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false)>]
type MetadataTypeAttribute = class
    inherit Attribute[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)>]
type MetadataTypeAttribute = class
    inherit AttributePublic NotInheritable Class MetadataTypeAttribute
Inherits Attribute- 继承
- 属性
示例
以下示例演示如何使用 MetadataTypeAttribute 将元数据类与实体分部类相关联。 该示例将 特性应用于 RequiredAttribute 数据字段,以显示如何在关联的元数据类中提供其他信息。
using System;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer
{
}
public class CustomerMetaData
{
    // Apply RequiredAttribute
    [Required(ErrorMessage = "Title is required.")]
    public object Title;
}
Imports System.Web.DynamicData
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(CustomerMetadata))> _
Partial Public Class Customer
End Class
Public Class CustomerMetadata
    ' Apply RequitedAttribute.
    <Required(ErrorMessage:="Title is required.")> _
    Public Title As Object
   
End Class
注解
使用 MetadataTypeAttribute 特性可以将类与数据模型分部类相关联。 在此关联的类中,提供数据模型中未包含的其他元数据信息。
例如,在关联的 类中,可以将 特性应用于 RequiredAttribute 数据字段。 这强制要求为字段提供值,即使数据库架构不需要此约束也是如此。
使用 特性, MetadataTypeAttribute 如下所示:
- 在应用程序中,创建一个文件,在其中创建要修改的数据模型分部类。 
- 创建关联的元数据类。 
- 将 MetadataTypeAttribute 特性应用于分部实体类,并指定关联的类。 
应用此属性时,必须遵守以下使用约束:
- 特性只能应用于类。 
- 派生类不能继承 特性。 
- 特性只能应用一次。 
构造函数
| MetadataTypeAttribute(Type) | 初始化 MetadataTypeAttribute 类的新实例。 | 
属性
| MetadataClassType | 获取与数据模型分部类关联的元数据类。 | 
| 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) |