On Thursday, 3 December 2015 19:56:17 UTC, Felipe Oriani 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. > > We do something similar, but all custom fields are in their own table.
Each entity that can have custom fields has an associated template, which defines a set of custom fields that can appear on that entity. (So you might have one template on "City", and another on "Person"). Each custom field has a numeric Id. The CustomFieldValues table links based on field Id, and entity Id (e.g. city Id). (At present we have one table per entity type but alternatives are possible). This avoids having to dynamically restructure your original table - which sounds like a never ending source of bugs to me. Searching/sorting by custom field value is all possible, as are indexes (and unique ones if you want them). There are probably disadvantages to doing it this way, if you're trying to do a very complex search/sort based on two or more of these custom fields - but in my case it's fine, and the fact that I'm not having to do anything very clever and have the table structure change under me outweighs that. -- 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.
