模型错误没有持久性

时间:2017-06-15 13:35:13

标签: c# .net fluent-nhibernate

首先,我用谷歌搜索并在这里阅读了类似的问题,其中没有帮助。 但也许我想念一些真正重要的东西?

这里有这个教程,试图坚持下去(但也许我想念一些真正重要的东西?) http://www.infoworld.com/article/3030212/application-development/how-to-work-with-fluent-nhibernate-in-c.html

所以,继承我的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连接字符串:

  <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;
        }
    }

堆栈追踪:

  

at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String   entityName)at   NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName,   对象obj)在   NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(对象   entity,String entityName,Object any,IEventSource source,   布尔值requireImmediateIdAccess)at   NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent   事件)   NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent   事件)   NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent   事件)   NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent   事件)在NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent   事件)在NHibernate.Impl.SessionImpl.Save(Object 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对该教程是正确的,它有错误

1 个答案:

答案 0 :(得分:0)

我又太傻了。 来上班,再次用谷歌搜索。

这里的答案帮助了我:no persister for: Fluent nHibernate Exception

所以,就我而言 .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductMap>())解决了这个问题。

谢谢大家!