“没有坚持者” NHibernate、NHibernate.Linq 和 Fluent 映射错误
我正在使用 Nhibernate 2.1.2.4000 GA 和 Nhibernate.Linq 1.0 以及从 github 上的 master 下载的最新版本的 FluentNhibernate。
我正在做一些测试,每当我尝试删除由 linq 查询检索的实体时,我都会收到此错误:
没有持久化: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);
}
}
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们都错过了!
抱歉,伙计们,感谢你们的帮助,但我只是想。
如果你注意RemoveAll()方法,你会发现我正在尝试删除“q”对象,它不是一个实体,而是一个IQueriable,而不是传递“employee”。这是缺乏关注。
正确的代码是:
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:
您确定提供给 FNH 的程序集 (
Assembly.GetExecutingAssembly()
) 实际上是包含您的映射的程序集吗?修改您的
Mappings
调用以包含ExportTo
方法,该方法会将 FNH 找到的所有映射导出到指定文件夹;检查该文件夹的内容并查看是否所有映射都在其中。如果是,那么很可能不是 FNH 问题,而可能是 Linq 提供商的问题(正如 Michael 所说)。您可以检查的另一件事是 NH 实际使用的 NHibernate 配置实例。为此,请使用
BuildConfiguration
而不是BuildSessionFactory
并检查结果;有一个 ClassMappings 集合(或其某种变体),其中应包含所有映射的实体。如果看起来不错,那么尝试使用 Criteria API 或 HQL 创建查询,看看这是否可以解决您的问题(在这种情况下,几乎可以肯定是 linq 提供程序)。
Are you certain the assembly you're supplying to FNH (
Assembly.GetExecutingAssembly()
) is actually the one containing your mappings?Modify your
Mappings
call to include theExportTo
method, which'll export any mappings FNH finds to a specified folder; check out the contents of that folder and see if all the mappings are in there. If they are, then chances are it's not a FNH issue and might be a problem with the Linq provider (as Michael said).Another thing you can check is the NHibernate Configuration instance that NH is actually using. To do that, use
BuildConfiguration
instead ofBuildSessionFactory
and inspect the result; there's aClassMappings
collection (or some variation of that), which should contain all the mapped entities.If that looks fine, then try creating your query using the Criteria API or HQL instead, see if that fixes your problem (and in that case it's almost certain to be the linq provider).
这可能只是 Linq 提供程序中的一个错误。您可能想要尝试针对最新的 NHibernate 主干(其中包括新的/不同的 Linq 提供程序)重现问题。或者,如果您(成功)从等式中删除 Fluent NHibernate,您可能(相对容易)针对 Linq 提供程序提交测试用例/错误报告。
It may just be a bug in the Linq provider. You may want to try to reproduce the problem against the latest NHibernate trunk which includes a new/different Linq provider. Alternatively, if you (successfully) remove Fluent NHibernate from the equation, you can probably (relatively easily) submit a test case / bug report against the Linq provider.