Database Reference
In-Depth Information
In this scenario, we are just providing a one-to-one mapping of the table values to the ob-
ject values. We were able to take a shortcut as we already had the EF entity, so we could
just copy and paste most of the pieces. So now we have an entity object and the mapping;
however, instead of just calling the EF context , we actually have to code the NHibern-
ate session . We will create NhibernateHelper.cs , which will contain the con-
nectionstring that points to the correct database. Instead of SQL Server, we can eas-
ily use MySQL or SQLite on an iPhone or Android device. We will create a configuration
for the session factory:
private static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(
@"Server=localhost\SQLExpress;Database=nservicebus;Trusted_Connection=True;")
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<MVCApp.Models.AuditExt2>())
.BuildSessionFactory();
}
Once we create the configuration, we will open the session and call the database objects in
a way that is similar to how we do it in EF. Most of the code of the new function is made
up by copying the NHibernate object into the previous EF object that is displayed on the
screen. The screen won't have to change, just the entity object that NHibernate used:
public ActionResult Audit()
{
List<MVCApp.Models.AuditExt> models = new
List<MVCApp.Models.AuditExt>();
using (var session =
NHibernateHelper.OpenSession())
{
var audits =
Search WWH ::




Custom Search