Share via


Attribute-Based Mapping (LINQ to SQL)

LINQ to SQL maps a SQL Server database to a LINQ to SQL object model by either applying attributes or by using an external mapping file. This topic outlines the attribute-based approach.

In its most elementary form, LINQ to SQL maps a database to a DataContext, a table to a class, and columns and relationships to properties on those classes. You can also use attributes to map an inheritance hierarchy in your object model. For more information, see How to: Generate the Object Model in Visual Basic or C# (LINQ to SQL).

Developers using Visual Studio typically perform attribute-based mapping by using the Object Relational Designer. For more information, see Object Relational Designer (O/R Designer) and Object Relational Designer (O/R Designer). You can also use the SQLMetal command-line tool, or you can hand-code the attributes yourself. For more information, see How to: Generate the Object Model in Visual Basic or C# (LINQ to SQL).

Note

You can also map by using an external XML file. For more information, see External Mapping Reference (LINQ to SQL).

The following sections describe attribute-based mapping in more detail. For more information, see the System.Data.Linq.Mapping namespace.

DatabaseAttribute Attribute

Use this attribute to specify the default name of the database when a name is not supplied by the connection. This attribute is optional, but if you use it, you must apply the Name property, as described in the following table.

Property

Type

Default

Description

Name

String

See Name

Used with its Name property, specifies the name of the database.

For more information, see DatabaseAttribute.

TableAttribute Attribute

Use this attribute to designate a class as an entity class that is associated with a database table or view. LINQ to SQL treats classes that have this attribute as persistent classes. The following table describes the Name property.

Property

Type

Default

Description

Name

String

Same string as class name

Designates a class as an entity class associated with a database table.

For more information, see TableAttribute.

ColumnAttribute Attribute

Use this attribute to designate a member of an entity class to represent a column in a database table. You can apply this attribute to any field or property.

Only those members you identify as columns are retrieved and persisted when LINQ to SQL saves changes to the database. Members without this attribute are assumed to be non-persistent and are not submitted for inserts or updates.

The following table describes properties of this attribute.

Property

Type

Default

Description

AutoSync

AutoSync

Never

Instructs the common language runtime (CLR) to retrieve the value after an insert or update operation.

Options: Always, Never, OnUpdate, OnInsert.

CanBeNull

Boolean

true

Indicates that a column can contain null values.

DbType

String

Inferred database column type

Uses database types and modifiers to specify the type of the database column.

Expression

String

Empty

Defines a computed column in a database.

IsDbGenerated

Boolean

false

Indicates that a column contains values that the database auto-generates.

IsDiscriminator

Boolean

false

Indicates that the column contains a discriminator value for a LINQ to SQL inheritance hierarchy.

IsPrimaryKey

Boolean

false

Specifies that this class member represents a column that is or is part of the primary keys of the table.

IsVersion

Boolean

false

Identifies the column type of the member as a database timestamp or version number.

UpdateCheck

UpdateCheck

Always, unless IsVersion is true for a member

Specifies how LINQ to SQL approaches the detection of optimistic concurrency conflicts.

For more information, see ColumnAttribute.

Note

AssociationAttribute and ColumnAttribute Storage property values are case sensitive. For example, ensure that values used in the attribute for the AssociationAttribute.Storage property match the case for the corresponding property names used elsewhere in the code. This applies to all .NET programming languages, even those which are not typically case sensitive, including Visual Basic. For more information about the Storage property, see DataAttribute.Storage.

AssociationAttribute Attribute

Use this attribute to designate a property to represent an association in the database, such as a foreign key to primary key relationship. For more information about relationships, see How to: Map Database Relationships (LINQ to SQL).

The following table describes properties of this attribute.

Property

Type

Default

Description

DeleteOnNull

Boolean

false

When placed on an association whose foreign key members are all non-nullable, deletes the object when the association is set to null.

DeleteRule

String

None

Adds delete behavior to an association.

IsForeignKey

Boolean

false

If true, designates the member as the foreign key in an association representing a database relationship.

IsUnique

Boolean

false

If true, indicates a uniqueness constraint on the foreign key.

OtherKey

String

ID of the related class

Designates one or more members of the target entity class as key values on the other side of the association.

ThisKey

String

ID of the containing class

Designates members of this entity class to represent the key values on this side of the association.

For more information, see AssociationAttribute.

Note

AssociationAttribute and ColumnAttribute Storage property values are case sensitive. For example, ensure that values used in the attribute for the AssociationAttribute.Storage property match the case for the corresponding property names used elsewhere in the code. This applies to all .NET programming languages, even those which are not typically case sensitive, including Visual Basic. For more information about the Storage property, see DataAttribute.Storage.

InheritanceMappingAttribute Attribute

Use this attribute to map an inheritance hierarchy.

The following table describes properties of this attribute.

Property

Type

Default

Description

Code

String

None. Value must be supplied.

Specifies the code value of the discriminator.

IsDefault

Boolean

false

If true, instantiates an object of this type when no discriminator value in the store matches any one of the specified values.

Type

Type

None. Value must be supplied.

Specifies the type of the class in the hierarchy.

For more information, see InheritanceMappingAttribute.

FunctionAttribute Attribute

Use this attribute to designate a method as representing a stored procedure or user-defined function in the database.

The following table describes the properties of this attribute.

Property

Type

Default

Description

IsComposable

Boolean

false

If false, indicates mapping to a stored procedure. If true, indicates mapping to a user-defined function.

Name

String

Same string as name in the database

Specifies the name of the stored procedure or user-defined function.

For more information, see FunctionAttribute.

ParameterAttribute Attribute

Use this attribute to map input parameters on stored procedure methods.

The following table describes properties of this attribute.

Property

Type

Default

Description

DbType

String

None

Specifies database type.

Name

String

Same string as parameter name in database

Specifies a name for the parameter.

For more information, see ParameterAttribute.

ResultTypeAttribute Attribute

Use this attribute to specify a result type.

The following table describes properties of this attribute.

Property

Type

Default

Description

Type

Type

(None)

Used on methods mapped to stored procedures that return IMultipleResults. Declares the valid or expected type mappings for the stored procedure.

For more information, see ResultTypeAttribute.

DataAttribute Attribute

Use this attribute to specify names and private storage fields.

The following table describes properties of this attribute.

Property

Type

Default

Description

Name

String

Same as name in database

Specifies the name of the table, column, and so on.

Storage

String

Public accessors

Specifies the name of the underlying storage field.

For more information, see DataAttribute.

See Also

Other Resources

Reference (LINQ to SQL)