Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let me suggest to override SchemaExport. There is private field accessor via reflection, it requires full trust mode. If this approach does not suit your needs, consider rewriting SchemaExport (relatively light class)</p> <pre><code> using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using FluentNHibernate.Mapping; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; namespace FluentNHib { public class Master { public int Id { get; set; } } public class Child { public int Id { get; set; } [MCIndex("A", 0)] public Master Master { get; set; } [MCIndex("A", 1)] public string Name { get; set; } } public class MCIndexAttribute : Attribute { public string indexName; public int indexOrder; public MCIndexAttribute(string indexName, int i) { this.indexName = indexName; this.indexOrder = i; } } public class MasterMap : ClassMap { public MasterMap() { Id(x => x.Id); } } public class ChildMap : ClassMap { public ChildMap() { Id(x => x.Id); References(x => x.Master).Index("A"); Map(x => x.Name).Index("A"); } } class MySchemaExport : SchemaExport { internal struct MCIndexField { internal int index; internal string Name; } internal class MCIndex { internal string IndexName; public readonly IList fields = new List(); public string Table; public void AddField(string name, int indexOrder) { fields.Add(new MCIndexField {index = indexOrder, Name = name}); } } private readonly Dictionary indexes = new Dictionary(); MCIndex ByName(string name, string table) { MCIndex result; if (!indexes.TryGetValue(name, out result)) { result = new MCIndex { IndexName = name }; indexes.Add(name, result); } return result; } public MySchemaExport(Configuration cfg) : base(cfg) { foreach (var type in typeof(ChildMap).Assembly.GetTypes()) { foreach (var prop in type.GetProperties()) { var attr = prop.GetCustomAttributes(typeof (MCIndexAttribute), true); if (attr.Length == 1) { var attribute = (MCIndexAttribute) attr[0]; ByName(attribute.indexName, type.Name).AddField(prop.Name, attribute.indexOrder); } } } var createSqlProp = typeof(SchemaExport).GetField("createSQL", BindingFlags.NonPublic | BindingFlags.Instance); var wasSql = createSqlProp.GetValue(this); var sb = new StringBuilder(); sb.AppendLine(""); foreach (var mcIndex in indexes) { sb.AppendLine(string.Format("create index {0} on {1} ({2})", mcIndex.Value.IndexName, mcIndex.Value.Table, mcIndex.Value.fields)); } createSqlProp.SetValue(this, wasSql + sb.ToString()); } } class Program { private static void BuildSchema(Configuration config) { new MySchemaExport(config) .Create(s => { Debug.WriteLine(s); }, true); } const string fileName = "c:\\temp\\temp.fdb"; private static string GetConnectionString() { const string userName = "sysdba"; const string password = "masterkey"; return String.Format("ServerType=1;User={0};Password={1};Dialect=3;Database={2}", userName, password, fileName); } private static FluentConfiguration Configurate() { var fbc = new FirebirdConfiguration(); return Fluently.Configure() .Database(fbc.ShowSql().ConnectionString(GetConnectionString())) .Mappings(m => m.FluentMappings .AddFromAssemblyOf() ) .ExposeConfiguration(BuildSchema); } static void Main(string[] args) { FluentConfiguration fluentConfiguration = Configurate(); Configuration cfg = fluentConfiguration.BuildConfiguration(); } } }</code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload