“没有坚持者” NHibernate、NHibernate.Linq 和 Fluent 映射错误

发布于 2024-08-18 20:30:47 字数 1633 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-08-25 20:30:47

我们都错过了!

抱歉,伙计们,感谢你们的帮助,但我只是想。

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

正确的代码是:

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

            var p = q.ToList();

            foreach (var employee in p)
            {
                _session.Delete(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:

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

            var p = q.ToList();

            foreach (var employee in p)
            {
                _session.Delete(employee);
            }
        }
自由如风 2024-08-25 20:30:47

您确定提供给 FNH 的程序集 (Assembly.GetExecutingAssembly()) 实际上是包含您的映射的程序集吗?

修改您的 Mappings 调用以包含 ExportTo 方法,该方法会将 FNH 找到​​的所有映射导出到指定文件夹;检查该文件夹的内容并查看是否所有映射都在其中。如果是,那么很可能不是 FNH 问题,而可能是 Linq 提供商的问题(正如 Michael 所说)。

Mappings(
  m => m.FluentMappings
          .AddFromAssembly(mappingsAssemly)
          .ExportTo(@"C:\"));

您可以检查的另一件事是 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 the ExportTo 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).

Mappings(
  m => m.FluentMappings
          .AddFromAssembly(mappingsAssemly)
          .ExportTo(@"C:\"));

Another thing you can check is the NHibernate Configuration instance that NH is actually using. To do that, use BuildConfiguration instead of BuildSessionFactory and inspect the result; there's a ClassMappings 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).

你的呼吸 2024-08-25 20:30:47

这可能只是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文