nhibernate - NHibernate、NHibernate.Linq 和 Fluent 映射的 "No persister for"错误

标签 nhibernate fluent-nhibernate linq-to-nhibernate

我正在使用 Nhibernate 2.1.2.4000 GA 和 Nhibernate.Linq 1.0 以及从 github 上的 master 下载的最新版本的 FluentNhibernate。

我正在做一些测试,每当我尝试删除由 linq 查询检索到的实体时,我都会收到此错误:

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



所有其他操作(插入、更新和选择)看起来都很好;

我的实体类:
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);
    }
}

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

配置:
Assembly mappingsAssemly = Assembly.GetExecutingAssembly();

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

和不起作用的代码:
public void RemoveAll()
{
    var q = from employee in _session.Linq<Employee>()
            select employee;

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

有什么想法吗?

最佳答案

我们都错过了!

抱歉各位,感谢您的帮助,但我只是想通了。

如果您注意 RemoveAll() 方法,您会发现我正在尝试删除不是实体而是 IQueriable 的“q”对象,而不是传递“员工”。这是缺乏关注。

正确的代码是:

public void RemoveAll()
{
var q = 来自 _session.Linq() 中的员工
选择员工;

var p = q.ToList();

foreach(p 中的 var 员工)
{
_session.Delete(员工);
}
}

关于nhibernate - NHibernate、NHibernate.Linq 和 Fluent 映射的 "No persister for"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2121094/

相关文章:

nhibernate - NHibernate 中的延迟加载

nhibernate - 如何使用 FluentNHibernate 在 ManyToMany 生成的表中添加额外字段

c# - 如何在 fluent nhibernate 映射中使用数据库过程

c# - NHibernate w/Fluent - 没有 : [class] 的持久性

c# - NHibernate 中无法识别的配置部分 applicationSettings

使用通用存储库的 NHibernate Linq Eager Loading

nhibernate - 当具有相同术语的 HQL 选择有效时,为什么此 HQL 删除失败?

Linq for NHibernate 和预加载的 fetch 模式

c# - 仅从集合中获取第一个元素

nhibernate - 这是使用 thenFetch() 加载多个集合的正确方法吗?