Connection

To access micron, you need to import Micron namespace.

using Micron;

Add Configuration

MariaDB (formerly known as MySQL) is the most popular database among web and app developers. Use MariaDB or MySQL for light web development.

To connect to a MySQL database or a MariaDB database:

MicronConfig config = new MicronConfig()
{
   DatabaseName = "bunifuschools"
};

MicronDbContext.AddConnectionSetup(config);

You can also setup multiple connections:

MicronConfig config = new MicronConfig()
{
   DatabaseName = "dabasename"
};

MicronConfig config2 = new MicronConfig()
{
    DatabaseName = "dabasename",
    Host="localhost",
    User="root",
    Password="mypass",
    Port="3306",
};
 
 MicronDbContext.AddConnectionSetup(config);
 MicronDbContext.AddConnectionSetup(config,"connection1");
 MicronDbContext.AddConnectionSetup(config2,"connection2");

It is recommended to add connection code at the main function of your app before you load other modules.

Creating a connection.

To create a connection you just create an instance of MicronDBContext

 MicronDbContext micron = new MicronDbContext();

This will create a connection using the default connection configuration (which is the first connection added)

To create a connection to a specific connection config. Just add the configuration name as parameter

MicronDbContext db = new MicronDbContext("connection1");

Direct connection

You can create a micron connection by passing all the connection options as parameters.

MicronDbContext db = new MicronDbContext("dbname","locallhost","toot","");

This method is not recommended since the model generator relies on MicronConfig instance to create a connection. But once you have models already generated, you can use this connection in production.

Last updated