Michael D. Kirkpatrick
Michael D. Kirkpatrick

Reputation: 488

Fluent NHibernate Mapping Issues

Orders Table

Id(x => x.ID, "ID");
...
HasMany<OrdersLineItems>(x => x.LineItems).KeyColumn("ID").Inverse().Cascade.All();

OrdersLineItems Table

Id(x => x.ID, "ID");
...
References(x => x.Orders, "OrdersID").ForeignKey("ID");

I am trying to set up a mapping where the ID from the Orders Table is referenced in OrdersID within OrdersLineItemsTable.

The problem I am running into is when I do: order.LineItems.Add(lineItem); within my code, I get a SQL error stating:

Cannot insert the value NULL into column 'OrdersID', table 'MyDatabase.dbo.OrdersLineItems'; column does not allow nulls. INSERT fails. The statement has been terminated.

I have monitored the SQL queries being executed and can see that the Orders record is first created and it bombs out when trying to create the OrdersLineItems record.

Any suggestions? Thanks in advance.

Upvotes: 1

Views: 123

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 886

My suggestion would be to use a foreign key in the KeyColum of HasMany instead of the primary key of the OrdersLineItems:

HasMany(x => x.LineItems).KeyColumn("FK_Orders").Inverse().Cascade.All();

Upvotes: 1

Related Questions