0

I can't seem to make my table's column order right with Fluent NHibernate.

Employee.cs

public class Employee : Entity
{
    #region Fields

    private EmployeeStatus _status;
    private DateTime _created;
    private DateTime? _updated;

    #endregion

    #region Constructor

    public Employee()
    {
        _status = EmployeeStatus.Active;
        _created = DateTime.Now;
    }

    #endregion

    #region Properties

    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual string LastName { get; set; }

    public virtual string FullName
    {
        get
        {
            return string.Format("{0} {1} {2}", FirstName, MiddleName, LastName);
        }
    }

    public virtual Gender Gender { get; set; }
    public virtual DateTime BirthDate { get; set; }
    public virtual Address HomeAddress { get; set; }

    public virtual EmployeeStatus IsActive
    {
        protected set { _status = value; }
        get { return _status; }
    }

    public virtual DateTime Created
    {
        protected set { _created = value; }
        get { return _created; }
    }

    public virtual DateTime? Updated
    {
        protected set { _updated = value; }
        get { return _updated; }
    }

    #endregion

    #region Overrides

    public override string ToString()
    {
        return FullName;
    }

    #endregion
}

HomeAddress.cs

public class Address : ValueObject<Address>
{
    #region Properties

    public virtual string City { get; private set; }
    public virtual string Zipcode { get; private set; }
    public virtual string AddressLine { get; private set; }

    #endregion

    #region Constructor

    public Address(string city, string zipcode, string addressLine)
    {
        City = city;
        Zipcode = zipcode;
        AddressLine = addressLine;
    }

    /// <summary>
    /// Required by NHibernate
    /// </summary>
    protected Address() { }

    #endregion

}

EmployeeMap.cs

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Guid();
        Map(x => x.FirstName);
        Map(x => x.MiddleName);
        Map(x => x.LastName);
        Map(x => x.BirthDate);
        Component(x => x.HomeAddress, m =>
        {
            m.Map(x => x.City);
            m.Map(x => x.Zipcode);
            m.Map(x => x.AddressLine);
        });
        Map(x => x.IsActive);
        Map(x => x.Created);
        Map(x => x.Updated);
    }
}

Based on above, I expected

Id 
FirstName
MiddleName
LastName
BirthDate
City
Zipcode
AddressLine
IsActive
Created
Updated

but instead NHibernate outputing this :

create table `Employee` (
        Id VARCHAR(40) not null,
       FirstName VARCHAR(255),
       MiddleName VARCHAR(255),
       LastName VARCHAR(255),
       BirthDate DATETIME,
       IsActive INTEGER,
       Created DATETIME,
       Updated DATETIME,
       City VARCHAR(255),
       Zipcode VARCHAR(255),
       AddressLine VARCHAR(255),
       primary key (Id)
    )

Is it possible to order table's column just from NHibernate / FluentNHibernate? Or should I create those columns structure manually in database first?

Thanks!

4

2 回答 2

1

在我看来,数据库列的顺序对数据库服务器来说毫无意义,你总是可以按任何需要的顺序选择列。NHibernate 或 Fluent NHibernate 显然是在组件属性之前编写简单属性的脚本。我个人不会担心它,但如果它对您很重要,那么您必须自己创建表或让 NHibernate 生成您可以编辑的创建脚本。

于 2013-09-23T23:40:11.677 回答
1

我设法通过以下方式为我的项目对 NHibernate 中的列进行排序:

  1. 导出 fluent 生成的 HBM 映射

  2. 将这些映射 XML 导入项目

  3. 更新 Fluent 配置以使用 XML 映射而不是类

花了大约一个小时才完成。

于 2015-11-06T04:40:28.040 回答