R – “No persister for” error with NHibernate, NHibernate.Linq and Fluent Mapping

fluent-nhibernatelinq-to-nhibernatenhibernate

I´m using Nhibernate 2.1.2.4000 GA with Nhibernate.Linq 1.0 and latest version of FluentNhibernate downloaded from master on github.

Im doing some tests and whenever I try to delete a entity retrieved by a linq query i´m getting this error:

No persister for: NHibernate.Linq.Query`1[[Employees.Core.Entities.Employee, Employees.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

All other operations (insert, update and select) looks fine;

My Entity class:

public class Employee
{
    public Employee()
    {
    }

    public virtual Int32 Id { get; private set; }
    public virtual String Name { get; set; }    

    public virtual String SayHello()
    {
        return String.Format("'Hello World!', said {0}.", Name);
    }
}

Mapping class:

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name)
            .Not.Nullable()
            .Length(50);
    }
}

Configuration:

Assembly mappingsAssemly = Assembly.GetExecutingAssembly();

return Fluently.Configure()
    .Database( MsSqlConfiguration.MsSql2008
                    .ConnectionString(connectionString)
                    .ProxyFactoryFactory(typeof(ProxyFactoryFactory))
                    .ShowSql())                            
    .Mappings( m => m.FluentMappings.AddFromAssembly(mappingsAssemly))
    .BuildSessionFactory();

And the code that does not work:

public void RemoveAll()
{
    var q = from employee in _session.Linq<Employee>()
            select employee;

    foreach (var employee in q.ToList())
    {
        _session.Delete(q);
    }
}

Any thoughts?

Best Answer

We all missed it!

Sorry guys and thanks for all your help but I just figured.

If you pay attention to the RemoveAll() method you will see that I´m trying to delete the "q" object which is not an entity, but a IQueriable instead of passing "employee". It was lack of attention.

The right code would be:

        public void RemoveAll()
        {
            var q = from employee in _session.Linq()
                    select employee;

            var p = q.ToList();

            foreach (var employee in p)
            {
                _session.Delete(employee);
            }
        }
Related Topic