Is it possible to set a default value on a property with NHibernate - nhibernate

I'm using Fluent NHibernate for my mappings and the SchemaExport class to the database schema.
Is it possible with NHibernate to set a default value for a property/column in the generated database schema?

Not to my knowledge, no - not in the generated schema.
You can just set the property in your constructor though.

It is definitely supported, both in XML mapping and in Fluent NHibernate.
For XML mapping, use the <column> child element of <property>, documented here.
For Fluent NHibernate, there's a method for fluent mapping, as described in this SO answer.

Related

FluentNHibernate automap to backing field

In our project we use MVC3 with a domain model and NHibernate as DAL.
We configure NHibernate with fluent configuration using auto mapping.
At the moment we are trying to devise a validation strategy.
We need validations that go beyond data annotations.
One place where we are sure to catch all attempts to alter (and corrupt) the data would be the property setters. As some checks involve querying the database, we do not want to do that when NHibernate restored objects form the database.
So for this to be a viable solution: Can we instruct FluentNhibernate to satandard AutoMap to the backing fields of the properties.
Thanks.
You can explicitly tell FNH to use a backing field: unfortunately AutoMapping support looks unlikely out-of-the box as there doesn't seem to be anything applicable in the configuration options.

How to create dynamic NHibernate mappings without generating HBM XML files?

I'm working on a dynamic application with NHibernate. My goal is to create dynamic entities (both class type and mapping xml file) based on some data. For example suppose I want to create a Person entity dynamically in runtime and on the fly.
I use Reflection.Emit to generate class type dynamically. For creating mapping dynamically I used Ayende's code.. But unfortunately this code does not work because mappings does not have Classes property. I tried to code as same as codes of Castle ActiveRecord and Fluent NHibernate but they generate HBM XML files. As I don't want to generate/create mapping files so I can not use these solutions.
Is there any way that like Ayende's solution not to be forced to generate HBM XML mapping files and just doing everything dynamically and in memory?
fluentnhibernate creates hbm in memory just to feed them to nhibernate. fluentnhibernate has the nice automapping feature with costumizable conventions, perfect for this situation. Also in FNH 2.0 they are working to skip hbm for better performance, but normally you'll never see the mappings outside memory.
Sample:
Assembly assembly = GetDynamicallyCreatedTypesAssembly();
ISessionFactory sf = Fluently.Configure()
.Database(...)
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(assembly)))
.BuildSessionFactory();
NHibernate 3.2 has a mapping-by-code layer that does what you want.
I'm not sure if dynamic classes will work, but it doesn't hurt to try.

NHIbernate 3 can't automap XmlDocument property

I'm trying to map a type where one of the properties is an XmlDocument but I get this error:
NHibernate.MappingException : An association from the table ChangeLog_TestAuditHistory refers to an unmapped class: System.Xml.XmlDocument
I am using Fluent NHibernate automappings. NHibernate version 3.0.0.4000 and Fluent NHibernate version 1.2.0.694. I know NHibernate is supposed to support xml columns, but I've never seen any examples using auto mappings.
That's probably a bug in Fluent, or a wrong convention... you probably need to tell it that XmlDocument is not an entity.
If you map it as a regular property, NH will use the correct mapping by default.

Nhibernate Mappings

Is there any way to get references to the mapping objects that NHibernate creates from the XML files? How about the ClassMap objects that FluentNhibernate creates? I wanted to create some query generation functions (for row counts, etc.) using this information. Since I went through the trouble of mapping it, I ought to have access to it in code, right?
Actually, Fluent ClassMaps are translated to XML.
The ISessionFactory exposes a GetAllClassMetadata method that is probably what you need.

Does FluentNH's PersistenceSpecification allow XML mappings to be tested?

Is it possible to use Fluent NHibernate's PersistenceSpecification to test NHibernate mappings done via XML?
The PersistenceSpecification takes session as its only parameter, so I do not see reason why not.