Domain.Build() requires the MySQL database (schema) to already exist. Is there any way to have DON create the schema before or during the Build? I would like to be able to run these commands:

DROP DATABASE IF EXISTS X

CREATE DATABASE X [plus any options such as CHARACTER SET]

asked Oct 29 '14 at 10:12

TimB's gravatar image

TimB
7558

edited Oct 29 '14 at 10:13


2 Answers:

Almost perfect. It seems that Domain.Build attempts to connect to the database BEFORE OnPrepare() runs. So the database must already exist. However, I can just run the drop and create before calling Domain.Build. Problem solved.

answered Nov 03 '14 at 13:34

TimB's gravatar image

TimB
7558

DO can't create schema or database automatically, it's only creates structure of schema. But! During domain building there are few points when you can customize domain building. You can implement your Upgrade handler and on prepare stage execute query what you need

For example, we have some model and handler


[HierarchyRoot]
public SomeEntity: Entity
{
  [Field, Key]
  public int Id{get; private set;}

[Field] public string TextField {get;set;} } // custom upgrade handler. // It provides you access to some points of upgrade process // You can manipulate upgrade using overloads of UpgradeHandler's virtual methods. public class CustomUpgradeHandler : UpgradeHandler { public override bool CanUpgradeFrom(string oldVersion) { return true; }

public override void OnPrepare() { using (var connection = new MySql.Data.MySqlClient.MySqlConnection()) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = "your query to MySql"; command.ExecuteNonQuery(); } connection.Close(); } } }

than we register types in domain configuration


...
var domainConfiguration = new DomainConfiguration(...);
domainConfiguration.Types.Register(typeof (SomeEntity));
domainConfiguration.Types.Register(typeof (CustomUpgradeHandler));
...
And that's all. DO will execute your manual query before building domain.

answered Oct 30 '14 at 02:05

Alexey%20Kulakov's gravatar image

Alexey Kulakov
66215

That's perfect! Thanks!

(Oct 30 '14 at 11:03) TimB TimB's gravatar image

Almost perfect. It seems that Domain.Build attempts to connect to the database BEFORE OnPrepare() runs. So the database must already exist. However, I can just run the drop and create before calling Domain.Build. Problem solved. Thanks!

(Oct 30 '14 at 12:06) TimB TimB's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×7
×2
×1
×1

Asked: Oct 29 '14 at 10:12

Seen: 2,122 times

Last updated: Nov 03 '14 at 13:34

powered by OSQA