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