Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate extract DBTransaction from Session
    primarykey
    data
    text
    <p>I have an legacy db where some stored proc calculates the row ids for all the tables. Now I want to overwrite the IIdentifierGenerator as hinted at this page <a href="http://www.richter-web.info/Wordpress/?p=132" rel="nofollow noreferrer">http://www.richter-web.info/Wordpress/?p=132</a> just to avoid the Id(x => x.id).GenereatedBy.Assigned.</p> <p>Now as I save some object,of course inside an Nhibernate transaction, named AppendixHierarchy, then as soon as it gets into ATKIdGenerator.Generate and it starts command.ExecuteNonQuery() I receive some exception ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.</p> <p>How can I extract the DB Transaction from the Nhibernate session object, in order to attach it to the command?</p> <p>I used the FluentMapping for the poco</p> <pre><code> public AppendixHierarchyMap() { Table("appendixHierarchy"); Id(x =&gt; x.id).GeneratedBy.Custom(typeof(ATKIdGenerator), a =&gt; a.AddParam("TableName", "appendixHierarchy")); ..... </code></pre> <p>And here is the Id Generator</p> <pre><code>public class ATKIdGenerator : IIdentifierGenerator, IConfigurable { private string TableName { get; set; } #region IIdentifierGenerator Members public object Generate(NHibernate.Engine.ISessionImplementor session, object obj) { IDbCommand command = new SqlCommand(); command.Connection = session.Connection; //transaction.Enlist(command); command.CommandType = CommandType.StoredProcedure; command.CommandText = "dbo.ups_GetNewId"; // Set input parameters var parm = new SqlParameter("@tableName", SqlDbType.VarChar); parm.Value = TableName; command.Parameters.Add(parm); // Set output parameter var outputParameter = new SqlParameter("@id", SqlDbType.Int); outputParameter.Direction = ParameterDirection.Output; command.Parameters.Add(outputParameter); // Set a return value var returnParameter = new SqlParameter("@RETURN_VALUE", SqlDbType.Int); returnParameter.Direction = ParameterDirection.ReturnValue; command.Parameters.Add(returnParameter); // Execute the stored procedure command.ExecuteNonQuery(); return (int)((SqlParameter)command.Parameters["@id"]).Value; } #endregion #region IConfigurable Members public void Configure(NHibernate.Type.IType type, IDictionary&lt;string, string&gt; parms, NHibernate.Dialect.Dialect d) { TableName = parms["TableName"]; } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload