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#.
Napomena
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.
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.
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.
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.
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.
The following table describes properties of this attribute.
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.
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.