更新:2007 年 11 月
将某个类指定为与数据库表相关联的实体类。
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := False, Inherited := False)> _ Public NotInheritable Class TableAttribute _ Inherits Attribute
Dim instance As TableAttribute
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public sealed class TableAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class, AllowMultiple = false, Inherited = false)] public ref class TableAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, Inherited = false) */ public final class TableAttribute extends Attribute
public final class TableAttribute extends Attribute
使用该属性可将某个类指定为与数据库表或视图相关联的实体类。LINQ to SQL 将具有 TableAttribute 属性的类视为持久性类。
LINQ to SQL 仅支持单表映射。即,实体类必须只映射到一个数据库表,且无法同时将数据库表映射到多个类。
您可以使用 TableAttribute 属性 (Attribute) 的 Name 属性 (Property) 来指定表名称,并可以选择使用架构名称来限定表名称。如果您未使用 Name 属性指定名称,则假定表名称与类名相同。
您可以选择使用架构名称来限定表名称。默认情况下,将 Name 字符串中第一个句点左侧的标记视为架构名称。该名称的其余部分视为表名称。提供程序根据需要对表名称使用括号。例如,用于 SQL Server 的 LINQ to SQL 提供程序可确保至少在需要时使用括号。
在某些情况下,由于 SQL Server 提供程序无法自动使用括号,因此您必须显式对属性使用括号。下表演示了一些示例。
情况
示例:标识符名称
示例:应在属性中使用的字符串
否则…
架构名称包含一个句点
架构:“A.B”
表:“C”
“[A.B].C”
假定第一个句点将架构名称与表名称分隔开。
架构/表名称以 @ 开头
“@SomeName”
“[@SomeName]”
假定是参数名。
架构以 [ 开头,并以 ] 结尾
“[Schema.Table]”
“[[Schema].[Table]]]”
未加括号的标识符类似于加括号的标识符。
表以 [ 开头,并以 ] 结尾
“[Table]”
“[[Table]]]”
<Table(Name:="Customers")> _ Public Class Customer ' ... End Class
[Table(Name = "Customers")] public class Customer { // ... }
Windows Vista, Windows XP SP2, Windows Server 2003