【问题标题】:No persister for Model error模型错误没有持久性
【发布时间】:2017-11-18 00:51:07
【问题描述】:

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

这里有这个教程,试着坚持下去(但也许我错过了一些真正重要的东西?) 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(String 实体名称)在 NHibernate.Impl.SessionImpl.GetEntityPersister(字符串实体名称, 对象 obj) 在 NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(对象 实体,字符串 entityName,对象任何东西,IEventSource 源, Boolean requiresImmediateIdAccess) 在 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 对教程的看法是正确的,其中有错误

【问题讨论】:

  • @Liam 哦,好吧,我只是习惯了._。以前从未遇到过这种情况
  • 不是一个答案,但你真的不应该在每次打开会话时都创建 SessionFactory,这让我怀疑那个教程。见Ensure NHibernate SessionFactory is only created once
  • @stuartd 哦,谢谢,是的。好吧,我想这只是刚刚开始的事情,所以他们认为这件事在这种情况下真的很重要,我想
  • @stuartd 你说得对,教程中的汇编引用错误!

标签: c# .net fluent-nhibernate


【解决方案1】:

我又太傻了。 来上班了,又google了。

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

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

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-30
    • 2011-05-14
    • 2013-06-21
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多