LinqConnect Documentation
In This Topic
    The DataContext
    In This Topic
    The DataContext
    LinqConnect Documentation
    The DataContext
    [email protected]

    This topic is based on: Creating Entity Classes. and Defining Relationships topics.

    The DataContext is the main controller that retrieves objects from the database and resubmits changes. You can use it in the same way that you would use an ADO.NET connection (the DataContext is initialized with a connection or connection string). The purpose of the DataContext is to translate your requests for objects into SQL queries made against the database and then assemble objects out of the results. The DataContext enables language-integrated query by implementing the same operator pattern as the standard query operators such as WHERE and SELECT.

    For example, let's define a DataContext class, which works with data from the CRM_DEMO database. We have defined three constructors for the DataContext initialization. First one initializes DataContext with an IDbConnection interface. Second one need an explicit definition of the connection string. Third is the default one who doesn't need any explicit declarations, we've initialized a connection string of the base class; Also we've defined a constructor that uses an IDBInterface instance for the DataContext creating. Also, in the given examples we have created the collections of the "Company" and "Order" entities. They are presented by the two instances of the Table<TEntity>.

    Note that the Devart.Data.Linq.DataContext class can use different data providers to connect to different DBMSs. Thus, when a DataContext instance is created, it should know what data provider to use. You can specify this data provider, e.g., by setting the ProviderAttribute class attribute for SystemDataContext. In the attribute, replace Devart.Data.Oracle.Linq.Provider.OracleDataProvider by the provider you actually use:

    namespace CrmDemoContext
    {
    
       [ProviderAttribute(typeof(Devart.Data.Oracle.Linq.Provider.OracleDataProvider))]
       partial class CrmDemoDataContext : DataContext
       {      
          public CrmDemoDataContext(System.Data.IDbConnection connection): base(connection) { }
          public CrmDemoDataContext(string connectionString): base(connectionString) { }
          public CrmDemoDataContext() : base("Server = Ora; User Id = CRM_DEMO; Password = CRM_DEMO;") { }   
          
          public Table<Company> Companies;
          public Table<Order> Orders;   
       }
    }
    Namespace CrmDemoContext
       <ProviderAttribute(typeof(Devart.Data.Oracle.Linq.Provider.OracleDataProvider))>
       Partial Public Class CrmDemoDataContext
                  Inherits DataContext
          
          Public Sub New(ByVal connectionString As String)
            MyBase.New(connectionString)
          End Sub
          Public Sub New(ByVal connection As System.Data.IDBConnection)
            MyBase.New(connection)
          End Sub
          Public Sub New()
            MyBase.New("Host = Ora; User Id = CRM_DEMO; Password = CRM_DEMO")
          End Sub
          
          Public Companies As Table(Of Company)
          Public Orders As Table(Of Order)
        End Class
    End Namespace    
        
    

    Use the following code if you want to get the database connection string from the configuration file.

       public CrmDemoDataContext() 
        : base(
            System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString) { }
    
       Public Sub New()        
          MyBase.New( _
            System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString)
       End Sub
    

    If you are developing web project, use the WebConfigurationManager class instead of the ConfigurationManager.