Search code examples
c#fluent-nhibernateumbracoumbraco-ucommerce

Nhibernate Save() not working


I have googled around everything for this problem and I am not sure what the problem is with my code. I would really appreciate if anyone can help me out. I am working with adding custom data to uCommerce 7.2.2, means extending the order details by adding a new tab.

For this I have created a new table like this -

CREATE TABLE [dbo].[uCommerce_OrderLineItemStatusAudit](
       [OrderLineItemStatusAuditId] [int] IDENTITY(1,1) NOT NULL,
       [LineOrderStatusId] [int] NOT NULL,
       [CreatedOn] [datetime] NOT NULL,
       [CreatedBy] [nvarchar](50) NOT NULL,
       [RefOrderId] [int] NOT NULL,
       [Message] [nvarchar](max) NULL,
       [Sku] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED 
(
       [OrderLineItemStatusAuditId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

The entity -

public class OrderLineItemStatusAudit : IEntity
    {
        public virtual int Id { get; set; }
        public virtual int OrderLineItemStatusAuditId { get; set; }
        public virtual int LineOrderStatusId { get; set; }
        public virtual DateTime CreatedOn { get; set; }
        public virtual string CreatedBy { get; set; }
        public virtual int RefOrderId { get; set; }
        public virtual string Message { get; set; }
        public virtual string Sku { get; set; }

    }

Mappings done for the table and the entity -

public class OrderLineItemStatusAuditMap : BaseClassMap<OrderLineItemStatusAudit>
    {
        public OrderLineItemStatusAuditMap()
        {
            Table("uCommerce_OrderLineItemStatusAudit");
            Id(x => x.OrderLineItemStatusAuditId, "OrderLineItemStatusAuditId");
            Map(x => x.LineOrderStatusId).Not.Nullable();
            Map(x => x.Sku).Not.Nullable();
            Map(x => x.CreatedOn).Not.Nullable();
            Map(x => x.CreatedBy).Not.Nullable();
            Map(x => x.RefOrderId).Not.Nullable();
            Map(x => x.Message).Nullable();
        }
    }

And this is the save method where I am getting the exception -

private void Save(OrderLine orderLine)
        {
            orderLine.Save();

            var orderLineStatus = new OrderLineItemStatusAudit
                {                
                    LineOrderStatusId = 2,
                    Sku = orderLine.Sku,
                    CreatedOn = orderLine.CreatedOn,
                    CreatedBy = "test",
                    RefOrderId = orderLine.OrderLineId,
                    Message = null
                };            
            _orderLineItemStatus.Save(orderLineStatus); // **getting exception here - Cannot insert null to LineOrderStatusId  column**

        }

I have referred this doc - Save custom data

Create Mapping Assembly Tag and Registering the assembly tag is also done.

Any help appreciated!!

Thanks in advance.


Solution

  • I got the solution for this problem and I think I should post it here too.

    NHibernate treats property names by convention and that "LineOrderStatusId" would be treated like a foreign key. In this case it is good to use a Reference in the mappings for that class instead and have NHibernate take care of the relation.

    If the column name has an Id postfixed then it would be treated as a reference to some parent table. This is the convention being used by uCommerce nHibernate mappings.

    Thanks