title: Wrong Mapping using FluentNHibernate NHibernate 3.3
body: <p>I get error when I working with "FluentNHibernate". Similar file in .xml working correct but I would like to use "FluentNHibernate" is much more easier. Do you know where I made mistake? and how to fix this issue?</p>
  
  <p><strong>Copy Exception</strong></p>
  
  <pre><code>NHibernate.MappingException was unhandled by user code
    Message=No persister for: TreningToWork.Models.Person
    Source=NHibernate
    StackTrace:
         at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
         at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
         at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
         at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
         at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
         at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
         at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
         at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
         at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
         at NHibernate.Impl.SessionImpl.Save(Object obj)
         at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 15
         at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
         at lambda_method(Closure , ControllerBase , Object[] )
         at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
         at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12()
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
    InnerException: 
  </code></pre>
  
  <p>Exception(more details):</p>
  
  <p><img src="https://i.stack.imgur.com/WcRYa.jpg" alt="enter image description here"></p>
  
  <p>Exception(more details):</p>
  
  <p><img src="https://i.stack.imgur.com/ndOq9.jpg" alt="enter image description here"></p>
  
  <p>FluentNHibernate (making this same as .xml) </p>
  
  <pre><code>using System.ComponentModel.DataAnnotations;
  using FluentNHibernate.Automapping;
  using FluentNHibernate.Automapping.Alterations;
  
  namespace TreningToWork.Models
  {
      public class Person 
      {
          [Key]
          public virtual int Id { get; set; }
          public virtual string Name { get; set; }
          public virtual string Surname { get; set; }
      }
      public class PersonMappingAltertation : IAutoMappingOverride&lt;Person&gt;
      {
          public void Override(AutoMapping&lt;Person&gt; mapping)
          {
              mapping.Table("Person");
              mapping.Id(x =&gt; x.Id, "Id").GeneratedBy.Identity();
              mapping.Map(x=&gt; x.Name, "Name").Column("Name").Not.Nullable();
              mapping.Map(x =&gt; x.Surname, "Surname").Column("Surname").Not.Nullable();
          }
      }
  }
  </code></pre>
  
  <p>Person Helper</p>
  
  <pre><code>using NHibernate;
  using NHibernate.Cfg;
  using TreningToWork.Models;
  
  namespace TreningToWork.ModelsHelper
  {
      public class PersonHelper
      {
          private static ISessionFactory _sessionFactory;
  
          private static ISessionFactory SessionFactory
          {
              get
              {
                  if (_sessionFactory == null)
                  {
                      var configuration = new Configuration();
                      configuration.Configure();
                      configuration.AddAssembly(typeof(Person).Assembly);
                      _sessionFactory = configuration.BuildSessionFactory();
                  }
                  return _sessionFactory;
              }
          }
  
          public static ISession OpenSession()
          {
              return SessionFactory.OpenSession();
          }
      }
  }
  </code></pre>
  
  <p>Person Repository</p>
  
  <pre><code>using NHibernate;
  using TreningToWork.Models;
  using TreningToWork.ModelsHelper;
  
  namespace NHibernateTutorial
  {
      public class PersonRepository
      {
          public void Add(Person newPerson)
          {
              using (ISession session = PersonHelper.OpenSession())
              {
                  using (ITransaction transaction = session.BeginTransaction())
                  {
                      session.Save(newPerson);
                      transaction.Commit();
                  }
              }
          }
  
          public Person GetPersonByName(string name)
          {
              using (ISession session = PersonHelper.OpenSession())
              {
                  var result = session.QueryOver&lt;Person&gt;().Where(x =&gt; x.Name == name).SingleOrDefault();
                  return result ?? new Person();
              }
          }
  
          public void Update(Person newPerson)
          {
              using (ISession session = PersonHelper.OpenSession())
              {
                  using (ITransaction transaction = session.BeginTransaction())
                  {
                      session.Update(newPerson);
                      transaction.Commit();
                  }
              }
          }
  
          public void Delete(Person newPerson)
          {
              using (ISession session = PersonHelper.OpenSession())
              {
                  using (ITransaction transaction = session.BeginTransaction())
                  {
                      session.Delete(newPerson);
                      transaction.Commit();
                  }
              }
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Edited file Error</strong></p>
  
  <pre><code>System.ArgumentNullException was unhandled by user code
    Message=Value cannot be null.
  Parameter name: Data Source
    Source=System.Data
    ParamName=Data Source
    StackTrace:
         at System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
         at FluentNHibernate.Cfg.Db.MsSqlConnectionStringBuilder.Create()
         at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.CreateProperties()
         at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.ConfigureProperties(Configuration nhibernateConfig)
         at FluentNHibernate.Cfg.FluentConfiguration.Database(IPersistenceConfigurer config)
         at TreningToWork.ModelsHelper.PersonHelper.get_SessionFactory() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 20
         at TreningToWork.ModelsHelper.PersonHelper.OpenSession() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 40
         at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 11
         at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
         at lambda_method(Closure , ControllerBase , Object[] )
         at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
         at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12()
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
    InnerException: 
  </code></pre>
  
  <p><strong>My changed Factory</strong></p>
  
  <pre><code>using FluentNHibernate.Cfg;
  using FluentNHibernate.Cfg.Db;
  using NHibernate;
  using TreningToWork.Models;
  
  namespace TreningToWork.ModelsHelper
  {
      public class PersonHelper
      {
          private static ISessionFactory _sessionFactory;
  
          private static ISessionFactory SessionFactory
          {
              get
              {
                  if (_sessionFactory == null)
                  {
                      _sessionFactory = Fluently.Configure()
                      .Database(MsSqlConfiguration.MsSql2008
                      .ConnectionString(c =&gt; c
                      .FromAppSetting("rafalConnectionString"))
                          //&lt;%$ ConnectionStrings:rafalConnectionString %&gt;
                      .ShowSql())
                      .Mappings(m =&gt; m
                      .FluentMappings.AddFromAssemblyOf&lt;Person&gt;())
                      .BuildSessionFactory();
                      // var configuration = new Configuration();
                      // configuration.Configure();
                      // configuration.AddAssembly(typeof(Person).Assembly);
                      // _sessionFactory = configuration.BuildSessionFactory();
                  }
                  return _sessionFactory;
              }
          }
  
          public static ISession OpenSession()
          {
              return SessionFactory.OpenSession();
          }
      }
  }
  </code></pre>
  
  <p>Really, I have got problem with configure. I add my project to this post. Tell me where I made mistake and how to fix this issue. I didn`t find good example for this and probably this project will go to youtube when will be finished for future. For other users.</p>
  
  <p>IF you understand all files in this post you don`t have to upload my project.</p>
  
  <p>Important files : </p>
  
  <p>Controller -> there is controller
  Models, ModelsHelper, Repository and View. If you open website my softweare have to put to database new table and data there. Now I have got problem with this. When I use .xml files everything working correct but I would like to use "FluentNHibernate" and this is a problem. </p>
  
  <p>File: <a href="http://www.sendspace.pl/file/b010fb8d696dd5ebcfcd34b" rel="nofollow noreferrer">My project download praca.zip</a></p>
  
  <p>Next error:</p>
  
  <pre><code>System.ArgumentNullException was unhandled by user code
    Message=Value cannot be null.
  Parameter name: Data Source
    Source=System.Data
    ParamName=Data Source
    StackTrace:
         at System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
         at FluentNHibernate.Cfg.Db.MsSqlConnectionStringBuilder.Create()
         at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.CreateProperties()
         at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.ConfigureProperties(Configuration nhibernateConfig)
         at FluentNHibernate.Cfg.FluentConfiguration.Database(IPersistenceConfigurer config)
         at TreningToWork.ModelsHelper.PersonHelper.get_SessionFactory() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 21
         at TreningToWork.ModelsHelper.PersonHelper.OpenSession() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 50
         at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 11
         at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
         at lambda_method(Closure , ControllerBase , Object[] )
         at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
         at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
         at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass15.&lt;InvokeActionMethodWithFilters&gt;b__12()
         at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
    InnerException: 
  </code></pre>