3

當我有一個傳統的數據庫結構是怎樣的順序列使用的SchemaExport

table t1 
(
    c0 bigint, // pk 
    c10 bigint, // deleted flag 
) 

table t2 
(
    c0 bigint, // pk 
    c1 bigint, // fk to t1.c0 
    c10 bigint, // deleted flag 
) 

和類

class Entity 
{ 
    public virtual long Id { get; private set; } 
    public virtual long Property { get; set; } 
} 

class Parent : Entity 
{ 
    public virtual ICollection<Child> Childs { get; private set; } 
} 

class Child : Entity { } 

與MappingByCode或FNH它映射後,SchemaExport工具會以錯誤的順序創建列。

table t2 
(
    c0 bigint, // pk 
    c10 bool, // deleted flag 
    c1 bigint, // fk to t1.c0 
) 

我該如何確保列以升序創建?

回答

3

通過源代碼後挖我想出了這個

configuration.BeforeBindMapping += (sender, e) => 
{ 
    // change to foreach if more than one classmapping per file 
    var c = e.Mapping.RootClasses[0]; 
    c.Items = 
     // sort everything with a column (simple property, reference, ...) 
     c.Items.OfType<IColumnsMapping>().OrderBy(cm => cm.Columns.First().name) 
     // concat everything that has no column (collection, formula, ...) 
     .Concat(c.Items.Where(o => !(o is IColumnsMapping))).ToArray(); 
};