What are the differences between HasOne and References in nhibernate?

HasOne creates a one-to-one mapping between tables for you. References creates a typical relational many-to-one relationship. More defined: a one-to-one relationship means that when one record exists in one table, it must (or can) have one and at most one record in the other referenced table. Example: User table and Options table (one user has … Read more

Fluent NHibernate – Create database schema only if not existing

You can just use SchemaUpdate instead, it will update the schema if it exists and create it if it does not: public NhibernateSessionFactory(IPersistenceConfigurer config) { _sessionFactory = Fluently.Configure(). Database(config). Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()). ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)). BuildSessionFactory(); } One caveat: SchemaUpdate does not do destructive updates (dropping tables, columns, etc.). It will only add … Read more

Override for fluent NHibernate for long text strings nvarchar(MAX) not nvarchar(255)

Adding this convention will set the default length for string properties to 10000. As others have noted, this will be a nvarchar(max) column. public class StringColumnLengthConvention : IPropertyConvention, IPropertyConventionAcceptance { public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) { criteria.Expect(x => x.Type == typeof(string)).Expect(x => x.Length == 0); } public void Apply(IPropertyInstance instance) { instance.Length(10000); } } Conventions can … Read more

The length of the string value exceeds the length configured in the mapping/parameter

This is a well known issue with NHibernate handling nvarchar(max), see : http://geekswithblogs.net/lszk/archive/2011/07/11/nhibernatemapping-a-string-field-as-nvarcharmax-in-sql-server-using.aspx For some years now, I have been file mapping nvarchar(max) columns to StringClob without encountering any problem : <property name=”myProp” column=”MY_PROP” not-null=”true” type=”StringClob” access=”property”></property> This link (from the comments) describes the fluent mapping required to fix this issue: https://www.tritac.com/nl/blog/fluent-nhibernate-nvarchar-max-fields-truncated-to-4000-characters/ Map(x => x.Description).CustomType(“StringClob”).CustomSqlType(“nvarchar(max)”);

Fluent NHibernate: How to create one-to-many bidirectional mapping?

To get a bidirectional association with a not-null foreign key column in the Details table you can add the suggested Owner property, a References(…).CanNotBeNull() mapping in the DetailsMap class, and make the Summary end inverse. To avoid having two different foreign key columns for the two association directions, you can either specify the column names … Read more

How to configure Fluent NHibernate to output queries to Trace or Debug instead of Console?

I can see from forum and blog posts everywhere that lots of others before me have looked for a way to get the SQL statements as they’re being prepared for execution. The answer typically is something along the lines of “you can’t”, or “you shouldn’t”. Whether I should or not, that’s what I wanted. After … Read more

How to update database table schemas with NHibernate schema generation?

The SchemaUpdate object provides database schema updating, by apparently generating and executing a series of SQL UPDATE statements (as well as constraint statements) when it’s void Execute(bool script, bool doUpdate) function is called. The SchemaUpdate class is in the NHibernate.Tool.hbm2ddl namespace, which can be found in the Nhibernate.dll file. SchemaUpdate is mentioned in chapter 15 … Read more

How can I have NHibernate only generate the SQL without executing it?

You can get the generated sql queries without execution with the following methods: For the NHibernate.Linq queries: public String GetGeneratedSql(System.Linq.IQueryable queryable, ISession session) { var sessionImp = (ISessionImplementor) session; var nhLinqExpression = new NhLinqExpression(queryable.Expression, sessionImp.Factory); var translatorFactory = new ASTQueryTranslatorFactory(); var translators = translatorFactory.CreateQueryTranslators(nhLinqExpression, null, false, sessionImp.EnabledFilters, sessionImp.Factory); return translators[0].SQLString; } For Criteria queries: public … Read more