流畅的nhibernate地图查找表作为枚举

时间:2013-01-09 10:42:38

标签: nhibernate fluent-nhibernate-mapping

我正在尝试使用以下命令将查找表映射到枚举: FluentNhibernate 1.3.0.733 NHibernate 3.3.1.4000

我无法加载对象。如果我删除枚举映射,我可以加载对象。

代码:

Order order = session.Get<Order>(id);

错误: 提供了错误类型的id。预期:订单+ OrderStatus,得到System.Int32

对象:

public class Order
{
    public enum OrderStatus
    {
        PaymentPending = 0
    }

    public virtual int Id { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual Address Address { get; set; }
    public virtual IList<OrderLine> OrderLines { get; set; }
    public virtual OrderStatus Status { get; set; }
    public virtual DateTime Created { get; set; }

    public Order()
    {
        OrderLines = new List<OrderLine>();
    }
}

映射:(我已将映射缩减到这些字段以进行测试)

public OrderMapping()
{
    Table("orders");
    Id(x => x.Id);
    Id(x => x.Status, "state_id").CustomType<Order.OrderStatus>().Not.Nullable();
    References(x => x.Address).Cascade.All().Column("address_id");
    References(x => x.Customer).Cascade.All().Column("customer_id");
}

表:

CREATE TABLE [order_states] (
    [id] INTEGER  NOT NULL PRIMARY KEY,
    [state] NVARCHAR(50)  NOT NULL
);

CREATE TABLE [orders] (
    [id] INTEGER  NOT NULL PRIMARY KEY,
    [customer_id] INTEGER  NOT NULL,
    [address_id] INTEGER  NOT NULL,
    [state_id] INTEGER  NOT NULL,
    [created] DATE,

    FOREIGN KEY(customer_id) REFERENCES customers(id),
    FOREIGN KEY(address_id) REFERENCES addresses(id),
    FOREIGN KEY(state_id) REFERENCES order_states(id)
);

我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于愚蠢的错误。 我将状态映射为id而不是属性。

正确的映射:

public OrderMapping()
        {
            Table("orders");
            Id(x => x.Id);
            Map(x => x.Status, "state_id").CustomType<Order.OrderStatus>().Not.Nullable();
            References(x => x.Address).Cascade.All().Column("address_id");
            References(x => x.Customer).Cascade.All().Column("customer_id");
        }