Attribute-Based Mapping

 

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#.

Developers using Visual Studio typically perform attribute-based mapping by using the Object Relational 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#.

System_CAPS_ICON_note.jpg Note

You can also map by using an external XML file. For more information, see External Mapping.

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

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.

PropertyTypeDefaultDescription
NameStringSee NameUsed with its Name property, specifies the name of the database.

For more information, see DatabaseAttribute.

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.

PropertyTypeDefaultDescription
NameStringSame string as class nameDesignates a class as an entity class associated with a database table.

For more information, see TableAttribute.

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.

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

Options: Always, Never, OnUpdate, OnInsert.
CanBeNullBooleantrueIndicates that a column can contain null values.
DbTypeStringInferred database column typeUses database types and modifiers to specify the type of the database column.
ExpressionStringEmptyDefines a computed column in a database.
IsDbGeneratedBooleanfalseIndicates that a column contains values that the database auto-generates.
IsDiscriminatorBooleanfalseIndicates that the column contains a discriminator value for a LINQ to SQL inheritance hierarchy.
IsPrimaryKeyBooleanfalseSpecifies that this class member represents a column that is or is part of the primary keys of the table.
IsVersionBooleanfalseIdentifies the column type of the member as a database timestamp or version number.
UpdateCheckUpdateCheckAlways, unless IsVersion is true for a memberSpecifies how LINQ to SQL approaches the detection of optimistic concurrency conflicts.

For more information, see ColumnAttribute.

System_CAPS_ICON_note.jpg 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.

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.

The following table describes properties of this attribute.

PropertyTypeDefaultDescription
DeleteOnNullBooleanfalseWhen placed on an association whose foreign key members are all non-nullable, deletes the object when the association is set to null.
DeleteRuleStringNoneAdds delete behavior to an association.
IsForeignKeyBooleanfalseIf true, designates the member as the foreign key in an association representing a database relationship.
IsUniqueBooleanfalseIf true, indicates a uniqueness constraint on the foreign key.
OtherKeyStringID of the related classDesignates one or more members of the target entity class as key values on the other side of the association.
ThisKeyStringID of the containing classDesignates members of this entity class to represent the key values on this side of the association.

For more information, see AssociationAttribute.

System_CAPS_ICON_note.jpg 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.

Use this attribute to map an inheritance hierarchy.

The following table describes properties of this attribute.

PropertyTypeDefaultDescription
CodeStringNone. Value must be supplied.Specifies the code value of the discriminator.
IsDefaultBooleanfalseIf true, instantiates an object of this type when no discriminator value in the store matches any one of the specified values.
TypeTypeNone. Value must be supplied.Specifies the type of the class in the hierarchy.

For more information, see InheritanceMappingAttribute.

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.

PropertyTypeDefaultDescription
IsComposableBooleanfalseIf false, indicates mapping to a stored procedure. If true, indicates mapping to a user-defined function.
NameStringSame string as name in the databaseSpecifies the name of the stored procedure or user-defined function.

For more information, see FunctionAttribute.

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

The following table describes properties of this attribute.

PropertyTypeDefaultDescription
DbTypeStringNoneSpecifies database type.
NameStringSame string as parameter name in databaseSpecifies a name for the parameter.

For more information, see ParameterAttribute.

Use this attribute to specify a result type.

The following table describes properties of this attribute.

PropertyTypeDefaultDescription
TypeType(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.

Use this attribute to specify names and private storage fields.

The following table describes properties of this attribute.

PropertyTypeDefaultDescription
NameStringSame as name in databaseSpecifies the name of the table, column, and so on.
StorageStringPublic accessorsSpecifies the name of the underlying storage field.

For more information, see DataAttribute.

Reference

Show: