0

First of all, I googled and been read similar problems here, non of them helped. But maybe I miss something what does really matter?

Got this tutorial here, tried to stick to it (But maybe I miss something what does really matter?) http://www.infoworld.com/article/3030212/application-development/how-to-work-with-fluent-nhibernate-in-c.html

So, heres my NHibernate Helper:

public static class NHibernateHelper
    {
        public static ISession OpenSession()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["WebAuthTest"].ConnectionString;

            ISessionFactory sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2012.ConnectionString(connectionString).ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
                .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true))
                .BuildSessionFactory();

            return sessionFactory.OpenSession();

        }
}

Web.config connection string:

  <connectionStrings>
    <add name="WebAuthTest" connectionString="data source=localhost;initial catalog=TestAuthDatabase;persist security info=True;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.0.4000" newVersion="4.1.0.4000" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

And it fails here, right after I call controller:

 public class TestController: ApiController
    {
        private readonly ITestService _testService;

        public TestController()
        {

        }

        public TestController(ITestService tst)
        {
            _testService = tst;
            using (ISession session = NHibernateHelper.OpenSession())

            {

                var product = new Product { Name = "Lenovo Laptop", Description = "Sample product" };

                session.SaveOrUpdate(product);

            }
        }

        public string Get()
        {
            var message = string.Format("The current data on the server is: {0}", _testService.TestFunction());
            return message;
        }
    }

Stack trace:

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.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)

Mapping and model right as they are in the article, but still:

public class Product

    {

        public virtual int Id { get; set; }

        public virtual string Name { get; set; }

        public virtual string Description { get; set; }

    }

    public class ProductMap : ClassMap<Product>
        {
            public ProductMap()
            {
                Id(x => x.Id);
                Map(x => x.Name);
                Map(x => x.Description);
                Table("Product");
            }

        }

UPD:

Solved. @stuartd was right about the tutorial, it has missteps in it

DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69
  • [Please don't put tags in question titles](https://stackoverflow.com/help/tagging) – Liam Jun 15 '17 at 13:36
  • @Liam oh, okay, I just get used to it ._. Never faced this before – DanilGholtsman Jun 15 '17 at 13:38
  • 1
    Not an answer, but you really shouldn't create the SessionFactory every time you open a session, and that makes me suspicious of that tutorial. See [Ensure NHibernate SessionFactory is only created once](https://stackoverflow.com/questions/2362195/ensure-nhibernate-sessionfactory-is-only-created-once?rq=1) – stuartd Jun 15 '17 at 13:40
  • @stuartd oh thanks, yep. well, I guess its just getting started things, so they tought that this thing doest really matter in this case, I presume – DanilGholtsman Jun 15 '17 at 13:45
  • There's no excuse for bad practice in a tutorial, getting started or not. It says to me that they don't know what they're talking about. – stuartd Jun 15 '17 at 13:58
  • 1
    @stuartd you were bloody right, it has wrong assembly reference there in tutorial! – DanilGholtsman Jun 16 '17 at 07:01

1 Answers1

0

I was too stupid, again. Came to work, googled again.

The answer here helped me: no persister for: Fluent nHibernate Exception

So, in my case .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductMap>()) solved the problem.

Thanks, everybody!

DanilGholtsman
  • 2,354
  • 4
  • 38
  • 69