使用SchemaExport时按名称排序列

时间:2012-05-07 13:02:18

标签: nhibernate nhibernate-mapping fluent-nhibernate-mapping mapping-by-code

我有像

这样的遗留数据库结构
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
)

如何确保按升序创建列?

1 个答案:

答案 0 :(得分: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();
};