Using the Column Attribute

,

Each property of the TimelineItem is decorated with a Column attribute, which tells the DataContext that the property is stored in a column by the same name. As with TableAttribute, you can use its Name property to specify a different name for the column in the database.

ColumnAttribute includes various other properties described in the following sections.

IsPrimaryKey

IsPrimaryKey is used to designate the member or members of the entity class that compose the primary key of the table. Composite keys are also supported by supplying more than one property decorated with a Column attribute with IsPrimaryKey set to true.

At least one member of an entity class must be designated as the primary key, or else the entity is deemed to be read-only. If an attempt is made to create, update, or delete a read-only entity, an InvalidOperationException is raised.

AutoSync

AutoSync is used to specify whether the column is automatically synchronized from the value generated by the database on insert or update commands.

The following is a list of valid values for this property:

Image Default

Image Always

Image Never

Image OnInsert

Image OnUpdate

If the column in your database provides a default value, set this property to OnInsert, OnUpdate, or Always.

CanBeNull

CanBeNull indicates whether a column can contain null values. This property is relevant when inserting data into the database, but not during the creation of the table.


Note

The DataContext.CreateDatabase method uses only the ColumnAttribute.DbType property, and not the ColumnAttribute.CanBeNull property when determining whether a column should allow null values. For this reason, you must specify whether a column can contain null values in the DbType property as well as setting CanBeNull to true.


If not specified, CanBeNull defaults to true if the member is a reference type; if the member is a value type, such as an int, then CanBeNull defaults to false.

DbType

DbType specifies the text that defines the column in a Transact-SQL create table statement. Using this property allows you to tailor a column data type to better match the data being stored in the column. If used correctly it can help to decrease the size of your database and may improve the speed of some queries.

For example, by not specifying the DbType of a string property, LINQ to SQL generates DDL for a column containing up to 4000 Unicode characters (NVARCHAR(4000)). If you, however, know that a field will always contain values of relatively the same length (within two characters difference), then NCHAR can be faster and more space efficient for data, and it may pay dividends to specify it using DbType.


Note

SQL CE is a Unicode-only database, so you must use Unicode data types when assigning the DbType property. In particular, use NVARCHAR instead of VARCHAR, and NCHAR instead of CHAR.


Expression

Expression allows you to automatically populate a field using a SQL expression. Use this property to define a column as containing computed values when you use DataContext.CreateDatabase to generate your database.

For example, if you want to create a column defined in SQL as InventoryValue AS UnitPrice * UnitsInStock, use the following Expression string:

UnitPrice * UnitsInStock


Note

LINQ to SQL does not support computed columns as primary keys.


IsDbGenerated

Entity members with IsDbGenerated are synchronized immediately after the row of data, representing the entity, is inserted into the database. The entity member’s value is set when DataContext.SubmitChanges completes.

IsDescriminator

IsDescriminator is used in conjunction with the InheritanceMapping attribute to provide an inheritance hierarchy in your data model. For more information, see the section “Mapping an Inheritance Hierarchy,” later in this chapter.

IsVersion

IsVersion allows you to designate an entity class member to be used for optimistic concurrency control (OCC). This property is discussed in the later section “Concurrency.”

UpdateCheck

UpdateCheck tells the data context when to detect change conflicts. This property is discussed in the later section “Concurrency.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.212.211