Ormlight Create MySql Database from code

Is there a way to create a MySql database directly through code using OrmLight?

Thank you.

Hi @Math, you can use ExecuteNonQuery and connect with elevated permissions. For example,

var adminDbFactory = new OrmLiteConnectionFactory(
    "server=localhost;user=root;port=3306;password=test", 
    MySqlDialect.Provider);
using var db = adminDbFactory.OpenDbConnection();
db.ExecuteNonQuery("CREATE DATABASE IF NOT EXISTS `hello`;");

Best not to use the same user/permission level for your app running and ability to create new databases if it isn’t absolutely required.

Hope that helps.

1 Like

This is perfect. Thank you.