0

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

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

代码:

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

错误:提供了错误类型的 id。预期:Order+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)
);

我究竟做错了什么?

4

1 回答 1

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");
        }
于 2013-01-09T11:29:24.303 回答