I've been able to manually "marry up" Fluent NHibernate with
NHibernate 2.1 (a long involved process that already exceeds 5 pages
of steps). Even after doing so this issue remained until I changed
storemap.cs to this:
namespace Examples.FirstProject.Mappings
{
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.Products)
.Cascade.All()
.WithTableName("StoreProduct");
HasMany(x => x.Staff)
.Inverse()
.Cascade.All()
;
}
}
}
To prove to myself that it was working with the above change (and also
to prove that it fails without that change), I changed method main to
separate out the session that writes the objects from the one that
retrieves them. Some partial code snippets follow:
using (var session = sessionFactory.OpenSession())
{
// populate the database
using (var transaction = session.BeginTransaction())
{
// create a couple of Stores each with some
Products and Employees
var barginBasin = new Store { Name = "Bargin
Basin" };
var superMart = new Store { Name = "SuperMart" };
<snip>
// save both stores, this saves everything else
via cascading
session.SaveOrUpdate(barginBasin);
session.SaveOrUpdate(superMart);
transaction.Commit();
}
session.Close();
}
// Do the Employees get saved when we save the Store
instance that they work in ?
using (var session = sessionFactory.OpenSession())
{
// retreive all stores and display them
using (session.BeginTransaction())
{
var stores = session.CreateCriteria(typeof(Store))
.List<Store>();
foreach (var store in stores)
{
WriteStorePretty(store);
}
}
Console.ReadKey();
}
}
My version of CreateSessionFactory looks like this (note the use of
ProxyFactoryFactory) and required adding an explicit Reference to the
NHibernate.ByteCode.LinFu assembly in order to make it work correctly:
/// <summary>
/// Sets up the required NHibernate session factory
/// </summary>
/// <returns>ISessionFactory instance</returns>
private static ISessionFactory CreateSessionFactory()
{
string connString = GetDatabaseConnectionString();
Console.WriteLine(connString);
ISessionFactory returnValue = null;
// Use Fluent NHibernate instead of an NHibernate XML
config file
returnValue = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.Is(connString))
.ShowSql()
.DefaultSchema("dbo")
.ProxyFactoryFactory
("NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu")
)
.Mappings(m => m
.FluentMappings.AddFromAssemblyOf<Program>()
)
//WARNING: will DROP/CREATE
tables .ExposeConfiguration (BuildSchema)
.BuildSessionFactory()
;
return returnValue;
}
The above also required my adding the following lines to
MsSqlConfiguration.cs to take advantage of NHibernate 2.1 support of
SQL Server 2008:
public static MsSqlConfiguration MsSql2008
{
get { return new MsSqlConfiguration
().Dialect<MsSql2008Dialect>(); }
> > > > > - Show quoted text -- Hide quoted text -