首先,我在这里搜索了类似的问题,其中没有一个帮助。 但也许我想念什么真的很重要?模型错误没有persister
来到这里本教程中,试图坚持下去(但也许我错过的东西是什么真的重要吗?) http://www.infoworld.com/article/3030212/application-development/how-to-work-with-fluent-nhibernate-in-c.html
所以,我的继承人NHibernate的助手:
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字符串:
<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>
,也未能在这里,之后我打电话控制器:
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;
}
}
堆栈跟踪:
在NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(字符串 的entityName)在 NHibernate.Impl .SessionImpl.GetEntityPersister(String entityName, Object obj)at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity,String e ntityName,对象什么,IEventSource源, 布尔requiresImmediateIdAccess)处 NHibernate.Event NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent 事件)在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent 事件)。 Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent 事件)在 NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate在NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent 事件)(SaveOrUpdateEvent 事件)在NHibernate.Impl.SessionImpl.Save(对象OBJ )
制图和模型正确的,因为它们是在文章中,但仍:
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:
解决。 @stuartd是正确的关于教程,它已经错过了它
[请不要把问题标题标签](https://stackoverflow.com/help/tagging) – Liam
@Liam哦,好吧,我只是习惯它。从来没有遇到过这个问题 – DanilGholtsman
不是答案,但是每次打开会话时都不应该创建SessionFactory,这让我对该教程产生怀疑。请参阅[确保NHibernate SessionFactory只创建一次](https://stackoverflow.com/questions/2362195/ensure-nhibernate-sessionfactory-is-only-created-once?rq=1) – stuartd