This project is read-only.

Cannot Flush Session, returning Null values

Topics: Customizing Orchard, General, Writing modules
Jul 17, 2012 at 1:48 AM
Edited Jul 17, 2012 at 1:57 AM

I have a part that has an activating filter that attaches the user part. I subsequently perform this function after posting from a Create view:

var customer = orchardSvc.ContentManager.New("Customer");
var user = customer.As();
customer.FirstName = "Foo";
customer.LastName = "Bar";
user.Email = "";

/* more properties set */

orchardSvc.ContentManager.Create(customer, VersionOptions.Published);



The result of this call after calling orchardSvc.ContentManager.Flush() is this:


could not insert: [Flashy.Web.Models.Customer#61][SQL: INSERT INTO Flashy_Web_CustomerRecord (FirstName, LastName, CreatedAt, Order_id, Id) VALUES (?, ?, ?, ?, ?)]



Jul 17, 2012 at 10:55 AM

Can you give the full error message and stack trace?

Jul 17, 2012 at 12:09 PM

@Bertrand, what is interesting is it says this on the inner error message:

{"The column name is not valid. [ Node name (if any) = ,Column name = Order_id ]"}

But when I look at the column in the database, it says that the column name is OrderRecord_Id. Odd.


Stack Trace: 

  at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Action.EntityInsertAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   at NHibernate.Engine.ActionQueue.ExecuteInserts()
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   at Orchard.Data.Repository`1.Create(T entity) in c:\Dev\v.0.1\Orchard.Source.1.4.2\src\Orchard\Data\Repository.cs:line 96
   at Orchard.Data.Repository`1.Orchard.Data.IRepository.Create(T entity) in c:\Dev\v.0.1\Orchard.Source.1.4.2\src\Orchard\Data\Repository.cs:line 36
   at Keeter.Startup.Controllers.FlashDeckAdminController.Create(FormCollection collection) in c:\Dev\v.0.1\Orchard.Source.1.4.2\src\Orchard.Web\Modules\Keeter.Startup\Controllers\FlashDeckAdminController.cs:line 46
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

The NHibernate.Exceptions.GenericADOException says:


{"could not insert: [Flashy.Web.Models.CustomerRecord#25][SQL: INSERT INTO Flashy_Web_CustomerRecord(FirstName, LastName, CreatedAt, Order_Id, Id) VALUES (?, ?, ?, ?, ?)]"}



Thank you for your assistance

Jul 17, 2012 at 12:44 PM

Well, yes, that would explain it. The naming of the column is wrong. It should adhere to the convention.

Jul 17, 2012 at 1:55 PM

@Bertrand : I appreciate your response, but I am lost to how my OrderRecord_Id column in my database became Order_Id in the SQL Query generated by NHib. My Migration is OrderRecord_Id, my OrderPartRecord is "public virtual int OrderRecord_Id," and my OrderPart mimics the same signature as the Record class.


Where is OrderRecord_Id turning into Order_Id? I am not finding Order_Id anywhere in my code but in the SQL statement that NHib created.

Jul 17, 2012 at 3:05 PM

Simple: you did not apply the convention. Your property has the wrong name, so it won't map to the column. Your property should be called Order, not OrderRecord. It's a convention. Apply it. Simple as that.

Jul 17, 2012 at 4:14 PM

Thank you for your patience, and I appreciate your continued work on Orchard, however, I believe that I need to solidify my knowledge on this subject. You stated that I need to follow a convention where my property should be Order and not OrderRecord. Please take a look at the below code and tell me where my confusion lies:

//apologize for errors, I am doing this from memory


public int Create(){
     SchemaBuilder.CreateTable("CustomerRecord", table =>

     ContentDefinitionManager.AlterTypeDefinition("Customer", builder => builder

     SchemaBuilder.CreateTable("OrderRecord", table=>

     ContentDefinitionManager.AlterTypeDefinition("Order", builder => builder
     return 1;


public virtual CustomerRecord Customer {get;set;}
public virtual int RoundedTotal {get;set;}


public CustomerRecord Customer { 
	get {return Record.Customer;}
	set{Record.Customer = value;}
public int RoundedTotal { 
	get {return Record.RoundedTotal;}
	set{Record.RoundedTotal = value;}

//with the above code, this doesn't work:

var order = orchardServices.ContentManager.New("Order");
var orderPart = order.As();
order.Customer = orchardServices.ContentManager.Get(18).Record; //get customer
order.RoundedTotal = 10;
Jul 17, 2012 at 6:57 PM

I don't think you're showing everything. Your customer does not seem to have a reference to an order, so where does this order_id come from??

Jul 17, 2012 at 8:58 PM

I apologize, I wrote the code in this post from memory and snippets I have emailed to myself (I am in training sessions all day and can't have my computer out all day) so I have been mainly going off of memory. There seems to be something fundamental I am not understanding and I believe that I need to go back to the N-N relationship docs and review.


In the mean time though, could you please tell me if my understanding of the Orchard N-N relationship is properly portrayed in the above code snippets?


thank you, and have a great day!

Jul 17, 2012 at 10:59 PM

Well, this seems to be 1-N.

Jul 17, 2012 at 11:56 PM

Of course you are right, thank you for pointing that out. Again, thank you for taking the time to respond. I have been nothing but impressed with the quality of people surrounding the Orchard project.

Jul 17, 2012 at 11:59 PM

You're too kind :)