我正在使用 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/