Problem adding Hierarchical data

Posts: 16
Joined: 22-Feb-2006
# Posted on: 30-Mar-2007 21:31:44   

Hi! I have two database tables Asset & Book. Book inherits from Asset in a 1:1 relationship. Unfortunately the following code fails with a FORIEGN KEY CONSTRAINT error (listed at bootom). Basically it's telling me that the Asset data needed to saved BEFORE the Book data. Neddless to say I thought the LLBL did this for me.

Anyway, I could use some advice as to how I should resolve this.

            BookEntity bookEntity = new BookEntity();

            //Data in the Asset Table
            bookEntity.Status = basicData.Status.ToString().ToUpper();
            bookEntity.Title = basicData.Title;
            bookEntity.SubTitle = basicData.SubTitle;
            bookEntity.Guid = Guid.NewGuid();

            //Data in the Book table
            bookEntity.PrimaryIsbn13 = basicData.PrimaryIsbn13;

            adapter.SaveEntity(bookEntity, true);


An exception was caught during the execution of an action query: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Book_Asset". The conflict occurred in database "Octavo", table "dbo.Asset", column 'Id'. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

Posts: 934
Joined: 12-Feb-2004
# Posted on: 31-Mar-2007 02:51:26   

Here is my code that executes and doesn't give an error. I'll explain how I setup my Asset and Book Tables and maybe that will help us get on the same page in order to recreate this issue.

            using(DataAccessAdapter adapter = new DataAccessAdapter())
                BookEntity bookEntity = new BookEntity();

                //Data in the Asset Table
                bookEntity.Name = "Applying Domain-Driven Design and Patterns";
                bookEntity.AssetId = Guid.NewGuid();

                //Data in the Book table
                bookEntity.Isbn = "0123456789";

                adapter.SaveEntity(bookEntity, true);

[Asset] AssetId (Guid, PK, No Default) Name

[Book] AssetId (Guid, PK, No Default) ISBN

Relationship with Asset.AssetId PK and Book.AssetId FK