LinqConnect Documentation
In This Topic
    Dynamic Database Creation and Dropping
    In This Topic

    LinqConnect provides the methods for the database management. It helps you to create, check existence, and drop the database. It can be very useful when you are creating a self-installing application.

    Mapping is enabled by using attribute-based mapping or an external mapping file to describe the structure of the relational database in the LinqConnect. In both ways, there is enough information about the relational database that you can create a new instance of the database using the DataContext.CreateDatabase method. The DataContext.CreateDatabase method creates a copy of the database what is limited by the information encoded in the object model. Mapping files and attributes from your object model might not encode everything about the structure of an existing database. Mapping information does not represent the contents of user-defined functions, stored procedures, triggers, or check constraints. This behavior is sufficient for a variety of databases. It is a reason why we don't perform the DatabaseExists method.

    Typical scenarios of the DataContext.CreateDatabase method usage are the following:

    Sometimes it is necessary to check if the database exists before creating it. If the database already exists it is necessary to drop it before creating a new one. The DataContext class provides the DeleteDatabase methods to help you with this process.

    The CreateDatabase and DeleteDatabase methods can have the ignoreErrors parameter that makes these methods to ignore errors.

    In given example we create new database on the server was selected in the DataContext constructor.

    CrmDemoContext.CrmDemoDataContext db 
        = new CrmDemoContext.CrmDemoDataContext("Server = Ora; User Id = CRM_DEMO; Password = CRM_DEMO;"); 
    
    db.CreateDatabase(true);
          
    
    Dim db As New CrmDemoContext.CrmDemoDataContext("Server = Ora; User Id = CRM_DEMO; Password = CRM_DEMO;")        
    
    db.CreateDatabase(True)
           
    

    If you want to check if the database exists on the server and delete it before creation, use the DeleteDatabase method with the ignoreErrors parameter. Call this method before calling CreateDatabase. This practice isn't so good because a big script will be executed, so it is better to try writing your own method, which checks if the database exists.

    CrmDemoContext.CrmDemoDataContext db 
        = new CrmDemoContext.CrmDemoDataContext("Server = Ora; User Id = CRM_DEMO; Password = CRM_DEMO;");
    
    db.DeleteDatabase(true);
    db.CreateDatabase(true);
          
    
    Dim db As New CrmDemoContext.CrmDemoDataContext("Server = Ora; User Id = CRM_DEMO; Password = CRM_DEMO;");        
    
    db.DeleteDatabase(True)
    db.CreateDatabase(True)
           
    

    Please note, that the database, which will be created, specific rights are needed to perfom operations with databases.

    DBMS Conditions
    Oracle The database schemas exist on the server and mentioned in connection string user has rights for DDL operations
    MySQL The mentioned user has rights for DDL scripts
    PostgreSQL The database schemas exist on the server and mentioned in connection string user has rights for DDL operations
    SQL Server The selected database exists on the server and the mentioned user has rights for DDL scripts
    SQL Server Compact The file of database exists and not read-only.
    SQLite The file of database is not read-only.