Saving records in multiple tables, not working.

Topics: Core, Customizing Orchard, Writing modules
Dec 4, 2014 at 7:37 AM
I have created two tables’ parent and child, TableA and TableB. I save record in parent table it gets saved, but if I try to save records into both the tables, the records doesn’t get save in any of the tables. It works fine for one table. My Migration and models are very simple:


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;
Here is how I am saving 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
If I comment out _tableB.Create(tableB) statement, _tableA.Create(tableA) gets executed just fine and the TableA is populated. But Both the statements doesn’t work. I can’t figure out what I am doing wrong here.
Dec 4, 2014 at 11:40 AM
What exception do you have in logs?
Dec 4, 2014 at 1:12 PM
I get the following exception:

could not insert: [Orchard.MyModuleName.Models.TableB][SQL: INSERT INTO
Orchard_MyModuleName_TableB (Status, LastModified, TableA_id, WorkflowRecord_id, ActivityRecord_id, UserPartRecord_id)
VALUES (?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]

Dec 4, 2014 at 7:43 PM
Why Status column is in your query ? It is not created in migration?!
Dec 5, 2014 at 2:39 AM
Attach a debugger, have it break at the exception, and view the exception details. It will show you exactly where you went wrong.
Dec 5, 2014 at 9:08 AM
Edited Dec 5, 2014 at 10:59 AM
I have fixed the exception. This time, no exception is occurring, but still can't save records in both of the tables. Records get save only into the first table, TableA, and if you try to save it into the second table as well, then both the tables are left empty. No exception, no nothing. :).

I think it makes transaction when saving records in multiple tables. So if records does not get saved in TableB, it removes records from TableA as well. What I don't understand is, if there is something wrong it should give exception. Why there is no exception?.

I am using SQL Server Express with Orchard by the way.

Dec 5, 2014 at 11:45 AM
Edited Dec 5, 2014 at 11:48 AM
I found the exception and have created a new Thread for it: Exception was found in the Error Log file, It wasn't occurring in the debugger.
Marked as answer by cloudsurfer on 12/10/2014 at 5:04 AM