DataContext Class

Represents the main entry point for the LINQ to SQL framework.

System.Object
  System.Data.Linq.DataContext

Namespace:  System.Data.Linq
Assembly:  System.Data.Linq (in System.Data.Linq.dll)

'Declaration
Public Class DataContext _
	Implements IDisposable

The DataContext type exposes the following members.

  NameDescription
Public methodDataContext(IDbConnection)Initializes a new instance of the DataContext class by referencing the connection used by the .NET Framework.
Public methodDataContext(String)Initializes a new instance of the DataContext class by referencing a file source.
Public methodDataContext(IDbConnection, MappingSource)Initializes a new instance of the DataContext class by referencing a connection and a mapping source.
Public methodDataContext(String, MappingSource)Initializes a new instance of the DataContext class by referencing a file source and a mapping source.
Top

  NameDescription
Public propertyChangeConflictsGets a collection of objects that caused concurrency conflicts when SubmitChanges was called.
Public propertyCommandTimeoutGets or sets a value that increases the time-out period for queries that would otherwise time out during the default time-out period.
Public propertyConnectionGets the connection used by the framework.
Public propertyDeferredLoadingEnabledGets or sets a value that indicates whether to delay-load one-to-many or one-to-one relationships.
Public propertyLoadOptionsGets or sets the DataLoadOptions associated with this DataContext.
Public propertyLogGets or sets the destination to write the SQL query or command.
Public propertyMappingGets the MetaModel on which the mapping is based.
Public propertyObjectTrackingEnabledGets or sets a value that indicates whether object tracking is enabled.
Public propertyTransactionGets or sets a local transaction for the .NET Framework to use to access the database.
Top

  NameDescription
Public methodCreateDatabaseCreates a database on the server.
Protected methodCreateMethodCallQuery(Of TResult)Infrastructure. Executes the table-valued database function associated with the specified CLR method.
Public methodDatabaseExistsDetermines whether the associated database can be opened.
Public methodDeleteDatabaseDeletes the associated database.
Public methodDisposeReleases all resources used by the current instance of the DataContext class.
Protected methodDispose(Boolean)Releases the unmanaged resources used by the DataContext class and optionally releases the managed resource.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodExecuteCommandExecutes SQL commands directly on the database.
Protected methodExecuteDynamicDeleteExecutes, inside delete override methods, to redelegate to LINQ to SQL the task of generating and executing dynamic SQL for delete operations.
Protected methodExecuteDynamicInsertExecutes, inside insert override methods, to redelegate to LINQ to SQL the task of generating and executing dynamic SQL for insert operations.
Protected methodExecuteDynamicUpdateExecutes, inside update override methods, to redelegate to LINQ to SQL the task of generating and executing dynamic SQL for update operations.
Protected methodExecuteMethodCallInfrastructure. Executes the stored database procedure or scalar function associated with the specified CLR method.
Public methodExecuteQuery(Type, String, Object())Executes SQL queries directly on the database.
Public methodExecuteQuery(Of TResult)(String, Object())Executes SQL queries directly on the database and returns objects.
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetChangeSetGets the modified objects tracked by DataContext.
Public methodGetCommandGets the information about SQL commands generated by LINQ to SQL.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTable(Type)Returns a collection of objects of a particular type, where the type is defined by the type parameter.
Public methodGetTable(Of TEntity)Returns a collection of objects of a particular type, where the type is defined by the TEntity parameter.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRefresh(RefreshMode, IEnumerable)Refreshes a collection of entity objects according to the specified mode.
Public methodRefresh(RefreshMode, Object)Refreshes an entity object according to the specified mode.
Public methodRefresh(RefreshMode, Object())Refreshes an array of entity objects according to the specified mode.
Public methodSubmitChangesComputes the set of modified objects to be inserted, updated, or deleted, and executes the appropriate commands to implement the changes to the database.
Public methodSubmitChanges(ConflictMode)Sends changes that were made to retrieved objects to the underlying database, and specifies the action to be taken if the submission fails.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodTranslate(DbDataReader)Converts an existing DbDataReader to objects.
Public methodTranslate(Type, DbDataReader)Converts an existing DbDataReader to objects.
Public methodTranslate(Of TResult)(DbDataReader)Converts an existing DbDataReader to objects.
Top

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft