If you have played around with the Fluent Mapping API you have most probably stumbled upon the issue of actually creating or migrating your database to the latest state. And while most tools suggest that schema operations should be performed during design time we give you the tools to either run them automatically or manually. 

All you need to do in order to get your schema up to date is instantiate a context object and then access its schema handler. I have prepared a simple code snippet that makes sure every time I run my console application my database schema will be updated with the latest changes.(However this is not recommended in a production environment as it could result in data loss).

class Program
{
    static void Main(string[] args)
    {
        using (var context = new NorthwindOAEntityDiagrams())
        {
            ISchemaHandler schemaHandler = context.GetSchemaHandler();
            if (schemaHandler.DatabaseExists() == false)
            {
                schemaHandler.CreateDatabase();
                schemaHandler.ExecuteDDLScript(schemaHandler.CreateDDLScript());
            }
            else
            {
                string ddl = schemaHandler.CreateUpdateDDLScript(new SchemaUpdateProperties());
                schemaHandler.ExecuteDDLScript(ddl);
            }
        }
 
    }
}

While this seems like a perfectly good solution while developing I will suggest creating a simple console application that outputs the update schema script in a file so that you can review it prior to executing on the server. 


Comments

Comments are disabled in preview mode.