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.
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.
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. |