Database First

To generate model code from an existing database, just go to Nuget Manager console and run the command bellow.

Before you run the command, its recommended to always save the project first.

Database first method

update-micron database-first

Micron will automatically set the default mode to database-first, running just update-micron without options will will execute the last used database-first mode

The command will look for database connections in your project then it will generate models matching your database schema and also inject relationships between model objects.

The examples in this doc , we will be using MicronInventory database sample. Follow the MySQL documentation on how to install and manage database.

Download Demo Database Here and import it to MySQL server as database MicronInventory

The ER diagram bellow shows micron inventory database, We will be using it for the examples.

After setting up your database. follow the steps bellow to Micron project

  1. Launch Visual studio and create a c# console project project (.Net Framework )

  2. Open Nuget manager and install Latest Micron.

  3. open go to Main Function in program.cs file and add micron connection.

  4. Open Nuget Manager Console and execute command: micron-update

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micron;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            MicronConfig config = new MicronConfig()
            {
                DatabaseName = "MicronInventory",
                Host="localhost",
                User="root",
                Port="3306"
            };
            MicronDbContext.AddConnectionSetup(config);
        }
    }
}

update-micron database-first

As seen on attached image bellow, Micron Model generator did find the connection from the code an it connected to the database to fetch the schema

Micron fetched the database schema then in generated the required files for the models.

As Seen on the image bellow. Micron created a folder called Models and it integrated the relationship files.

Congratulations!.. Now you are very ready for fast development. :-)

Generated models are on namespace using Data.Models (default);

Sample Model Customers.cs

Last updated