LINQ DataContext Class
Posted by Tihomir Ivanov on 07 November 2008 06:47
Rating: 0.00
The DataContext class is the core channel that you use to work with (for example,
to query or update) your database. For each LTS DBML file you add to your solution, a new
DataContext will be created.
The DataContext class is a generated partial class that exists in your AdventureWorks.
designer.cs file. In this example, your partial class is called AdventureWorksDataContext, which
is based on the name that you gave the DBML file. As you can see in the example below, the DataContext
class contains the properties and methods for interacting with the table(s) that have been added
to the designer.
In addition to the properties and methods in the AdventureWorksDataContext class, you
can also see in the example below that the System.Data.Linq.DataContext is used as the base class. This
base class is part of the LINQ framework and contains the contract and implementation logic
to work with the database and the entities in the database. Some examples of what is in the
DataContext class include Refresh, CreateDatabase, GetTable, and SubmitChanges.
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[System.Data.Linq.Mapping.DatabaseAttribute(Name="AdventureWorks")]
public partial class AdventureWorksDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource
mappingSource = new AttributeMappingSource ();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertDepartment(Department instance);
partial void UpdateDepartment(Department instance);
partial void DeleteDepartment(Department instance);
#endregion
static AdventureWorksDataContext()
{
}
public AdventureWorksDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public AdventureWorksDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public AdventureWorksDataContext(string connection,
System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public AdventureWorksDataContext(System.Data.IDbConnection connection,
System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public AdventureWorksDataContext() :
base(global::
LTS_Harness.Properties.Settings.Default.
AdventureWorksConnectionString, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Department> Departments
{
get
{
return this.GetTable<Department>();
}
}
}
Comments:
No comments yet.