2017-06-15 69 views
0

首先,我在這裏搜索了類似的問題,其中沒有一個幫助。 但也許我想念什麼真的很重要?模型錯誤沒有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是正確的關於教程,它已經錯過了它

+0

[請不要把問題標題標籤](https://stackoverflow.com/help/tagging) – Liam

+0

@Liam哦,好吧,我只是習慣它。從來沒有遇到過這個問題 – DanilGholtsman

+1

不是答案,但是每次打開會話時都不應該創建SessionFactory,這讓我對該教程產生懷疑。請參閱[確保NHibernate SessionFactory只創建一次](https://stackoverflow.com/questions/2362195/ensure-nhibernate-sessionfactory-is-only-created-once?rq=1) – stuartd

回答

相關問題