Would session.Flush() between session.Save() and
session.Connection.CreateCommand() work out for you?
On Dec 4, 2015 2:56 AM, "Felipe Oriani" <[email protected]> wrote:

> Hello guys, I have a problem using NHibernate, but first let me explain
> the scenario.
>
> We have a web application which allows the user to manage fields over the
> entities. For sample, we have a City table with fixed columns (Id, Name and
> State), then user can define new columns on the entity (city) by a
> configuration scren we have developed. This configuration screen runs some
> sql statements to manage the fields (Alter Table to create an drop columns
> on the tables) which we call as "flex field".
>
> However, these flex fields are no mapped by nhibernate, so, we need to
> manage the information on these fields manually by sql statement. The code
> bellow shows how we do it in a scenario when we are creating a new entity
> (city):
>
>    // start a transaction from asp.net mvc attribute (ActionExecuting)
>    var transaction = session.BeginTransaction();
>
>    // save a new entity on the data layer
>    session.Save(city);
>
>    // get a IDbCommand instance to manage the flex fields
>    var cmd = session.Connection.CreateCommand();
>
>    // define the sql update to update the flex fields passing the id of
> the entity
>    cmd.CommandText = "update city set flex1=@flex1, flex2=@flex2 where id
> = @id";
>
>    // define all the parameters on the cmd here...
>
>    // add the command on the nhibernate transaction object
>    transaction.EnList(cmd);
>
>    // execute the command
>    cmd.ExecuteNonQuery();
>
>    // commit the transaction on the asp.net mvc attribute (ActionExecuted)
>    transaction.Commit();
>
> When we run this code, everything executes without any error. If we are
> updating a existent entity (City) the flex fields are updated fine, but the
> problem is when we are inserting a new entity (City), the update statement
> executed by command is not being executed, and we get all the flex field
> with null values.
>
> I supose when we are inserting, the command is been performed before the
> insert (on commit). Is there any way to force the commands on EnList
> Transaction be executed after all others statements, but on the same
> transaction?
>
> Thank you.
>
>
>
>
> --
> ______________________________________
> Felipe B Oriani
> [email protected]
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to