How do you map an enum as an int value with fluent NHibernate?

The way to define this convention changed sometimes ago, it’s now : public class EnumConvention : IUserTypeConvention { public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) { criteria.Expect(x => x.Property.PropertyType.IsEnum); } public void Apply(IPropertyInstance target) { target.CustomType(target.Property.PropertyType); } }

Persist Data by Programming Against Interface

Your repository should accept BankAccount – not IBankAccount because Linq-to-sql doesn’t know what is IBankAccount and compiler doesn’t allow you to store it without casting it first to BankAccount (that can obviously fail at runtime if IBankAccount instance is not a BankAccount). Once you have BankAccount you simply call: Context.BankAccounts.Add(account); Context.SubmitChanges();

Minimal and correct way to map one-to-many with NHibernate

Just few hints, summarizing the most suitable standards I found out when working with NHibernate. 1) If there is a bi-directional reference in persitence (DB column), express it in C# code bi-directional as well. Other words, if a child has reference to parent, parent should have reference to child. public class Employee { … public … Read more

How does TransactionScope roll back transactions?

Essentially TransactionScope doesn’t track your Adapter’s, what it does is it tracks database connections. When you open a DB connection the connections will looks if there is an ambient transaction (Transaction Scope) and if so enlist with it. Caution if there are more the one connection to the same SQL server this will escalate to … Read more

What’s the difference between session.Merge and session.SaveOrUpdate?

This is from section 10.7. Automatic state detection of the Hibernate Reference Documentation: saveOrUpdate() does the following: if the object is already persistent in this session, do nothing if another object associated with the session has the same identifier, throw an exception if the object has no identifier property, save() it if the object’s identifier … Read more

many-to-many with extra columns nhibernate

The many-to-many, without the explicit mapping of the pairing table as an entity – is in NHibernate of course suported. So, in case, that the Date column is autogenerated, or nullable (does not have to be inserted by app/NHiberante), we can do it like here: 6.8. Bidirectional Associations <class name=”User”> <id name=”Id” column=”Uid”/> … <bag … Read more

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

While similar names, the usage is different. I. Projections.distinct(Projections.property(“id”)); this statement would be translated into SQL Statement. It will be passed to DB Engine and executed as a SQL DISTINCT. See: 17.9. Projections, aggregation and grouping so e.g. this example: List results = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.distinct(Projections.property(“id”)) ) ) .list(); would seems like: SELECT … Read more