FluentNHibernate表的列顺序

时间:2013-09-23 17:25:41

标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping

我似乎无法使用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);
    }
}

基于上述,我预计

Id 
FirstName
MiddleName
LastName
BirthDate
City
Zipcode
AddressLine
IsActive
Created
Updated

但是NHibernate输出了这个:

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)
    )

是否可以仅从NHibernate / FluentNHibernate订购表的列?或者我应该先在数据库中手动创建这些列结构吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

  1. 导出流畅生成的HBM映射

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

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

  4. 花了大约一个小时的时间。