NHibernate.TransientObjectException : object references an unsaved transient instance

Topics: Core, Customizing Orchard, Writing modules
Dec 5, 2014 at 12:31 PM
Edited Dec 5, 2014 at 12:48 PM
I am getting the following exception upon saving the record in the table:
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Orchard.Workflows.Models.WorkflowRecord, Entity: Orchard.Workflows.Models.WorkflowRecord
Here is the code


public class TableA
        public virtual int Id { get; set; }
        public virtual ContentItemRecord ContentItemRecord { get; set;  }

public class TableB
        public virtual int Id { get; set; }
        public virtual TableA TableA { get; set; }
        public virtual WorkflowRecord WorkflowRecord { get; set; }
        public virtual ActivityRecord ActivityRecord { get; set; }
        public virtual UserPartRecord UserPartRecord { get; set; }        


public class Migrations : DataMigrationImpl {

        public int Create() {
            SchemaBuilder.CreateTable("TableA", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())                .Column<int>("ContentItemRecord_Id"));      
SchemaBuilder.CreateTable("TableB", table => table
                .Column<int>("Id", column => column.PrimaryKey().Identity())
            ContentDefinitionManager.AlterPartDefinition("MyPart", part => part.Attachable(true));
            return 1;

Adding Records

private readonly IRepository<TableA> _tableA;  
private readonly IRepository<TableB> _tableB;
public MyConstructor(
IRepository<TableA> tableA,
IRepository<TableB> tableB
     _tableA = tableA; 
    _tableB = tableB;                    
var tableA = new TableA
                ContentItemRecord = contentItem.Record                  
var tableB = new TableB
                            ActivityRecord = activityContext.Record,
                            WorkflowRecord = workflowContext.Record,
                            LastModified = DateTime.Now,
                            TableA = tableA
The exception occurs on adding the record in TableB.

Dec 5, 2014 at 1:10 PM
Edited Dec 5, 2014 at 1:10 PM
You have to do exactly what exception has told you.
  1. Flush it
put _tableA.Flush(); after _tableA.Create(tableA);

  1. Map as cascade
Use mapping overrides to additionally define that property TableA is cascaded
Dec 8, 2014 at 11:34 AM
Edited Dec 8, 2014 at 11:51 AM
Hello Daniel, Thanks for your time.

I have tried calling Flush() right after Create(TableA), but it didn't work. But I have found what the problem is. As exception shows it has some problem with workflowRecord object. So I inspected this object and the WorfkflowRecord.Id was 0. I tried replacing it with a non-zero number, and it worked. And the recrord was save into the database.

I don't understand why WorkflowRecord is 0. and Event if it is "0" why It gives the above mentioned exception on saving the record.

Dec 8, 2014 at 1:32 PM
It sounds like the workflow record you're referencing hasn't been stored to the database yet. Find out at which point that record is saved, and save your own record after that.
Marked as answer by cloudsurfer on 12/9/2014 at 3:22 AM