2

I have got two kind of entities, comments and tasks. Both types can be bookmarked by a user. So I have created a table called 'Bookmarks':

CREATE TABLE [dbo].[Bookmarks] (
[ID] [int] IDENTITY(1,1) NOT NULL,
[FKUserID] [int] NOT NULL,
[FKCommentID] [int] NULL,
[FKTaskID] [int] NULL)

The mapping of these relations for my User entity looks like this:

var bookmarks = Component(x => x.Bookmarks, x => x.HasManyToMany(b => b.Comments)
.Table("Bookmarks")
.Access.CamelCaseField()
.ParentKeyColumn("FKUserID")
.ChildKeyColumn("FKCommentID")
.AsBag()
.LazyLoad());

bookmarks.HasManyToMany(b => b.Tasks)
    .Table("Bookmarks")
    .Access.CamelCaseField()
    .ParentKeyColumn("FKUserID")
    .ChildKeyColumn("FKTaskID")
    .AsBag()
    .LazyLoad();

That all would work nicely I think, but not when the database objects are created by scratch from NHibernate itself. Then I get the nice error message

NHibernate.Exceptions.GenericADOException : could not insert collection: [Domain.Model.User.Domain.Model.User.Bookmarks.Tasks#1][SQL: INSERT INTO Bookmarks (FKUserID, FKTaskID) VALUES (@p0, @p1)] ----> System.Data.SqlClient.SqlException : Cannot insert the value NULL into column 'FKCommentID', table 'xyzautomatedtests.dbo.Bookmarks'; column does not allow nulls. INSERT fails. The statement has been terminated.

Sure I could have been using two tables instead of this one, but isn't there a way to get this running? Unfurtunately I can't find a way to set these two child columns in the configuration to be nullable.

Any hints?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
asp_net
  • 3,567
  • 3
  • 31
  • 58

1 Answers1

2

Double check your table definition. The SQLException comes straight from the database, so the problems that you're having are from the database, not from .Net.

Just make sure that the FKCommentID column in your Bookmarks table is actually Nullable.

Richard
  • 6,215
  • 4
  • 33
  • 48