Skip to content

Instantly share code, notes, and snippets.

@pofider
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pofider/3cae0c423eb084e4bf79 to your computer and use it in GitHub Desktop.
Save pofider/3cae0c423eb084e4bf79 to your computer and use it in GitHub Desktop.
Adding where condition into NHibernate update statements
public class MultitenantEntityPersister : SingleTableEntityPersister
{
public MultitenantEntityPersister(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
: base(persistentClass, cache, factory, mapping)
{
}
protected override NHibernate.SqlCommand.SqlCommandInfo GenerateUpdateString(bool[] includeProperty, int j, bool useRowId)
{
var index = base.GetPropertyIndex("TenantId");
foreach (bool p in includeProperty)
{
if (!p)
index--;
}
var result = base.GenerateUpdateString(includeProperty, j, useRowId);
var tenantWhereBuilder = new SqlStringBuilder();
tenantWhereBuilder
.Add(" AND ")
.Add("TenantId")
.Add(" = ")
.Add(Parameter.WithIndex(index));
result = new SqlCommandInfo(result.Text.Append(tenantWhereBuilder.ToSqlString()), result.ParameterTypes);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment