Domanda

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!

È stato utile?

Soluzione

In my opinion, database column ordering is meaningless to the database server and you can always select the columns in any order needed. NHibernate, or Fluent NHibernate is obviously scripting the simple properties before the component properties. Personally I wouldn't worry about it, but if it's important to you then you'll have to create the table yourself or have NHibernate generate the create script that you can then edit.

Altri suggerimenti

I managed to sequence the columns in NHibernate for my project by:

  1. Exporting the fluent-generated HBM mappings

  2. Import those mapping XMLs into project

  3. Update Fluent configuration to use XML mappings instead of classes

Took about an hour to do.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top