0

我在样本中有 3 个表。我要加入 2 个表,但我有问题以下是我的代码:

<?xml version="1.0"?>

</configSections>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=(local);database=human;Integrated Security=SSPI;</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

using System.Collections.Generic; 
using System.Text; 
using System; 


namespace human {

public class TblStudent {
    public TblStudent() { }
    public virtual int ID { get; set; }
    public virtual IList<TblTerm> TblTerms { get; set; }
    public virtual string name { get; set; }
    public virtual string lastname { get; set; }
}
}


using System; 
using System.Collections.Generic; 
using System.Text; 
using FluentNHibernate.Mapping;

namespace human {


public class TblStudentMap : ClassMap<TblStudent> {

    public TblStudentMap() {
        Table("TblStudent");
        LazyLoad();
        Id(x => x.ID).GeneratedBy.Identity().Column("ID");
        Map(x => x.name).Column("name");
        Map(x => x.lastname).Column("lastname");
        HasMany(x => x.TblTerms);
    }
}
}

using System.Collections.Generic; 
using System.Text; 
using System; 


namespace human {

public class TblTerm {
    public TblTerm() { }
    public virtual int ID { get; set; }
    public virtual TblLesson TblLesson { get; set; }
    public virtual TblStudent TblStudent { get; set; }
}
}

using System; 
using System.Collections.Generic; 
using System.Text; 
using FluentNHibernate.Mapping;

namespace human {


public class TblTermMap : ClassMap<TblTerm> {

    public TblTermMap() {
        Table("TblTerm");
        LazyLoad();
        Id(x => x.ID).GeneratedBy.Identity().Column("ID");
        References(x => x.TblLesson).Column("LessonID");
        References(x => x.TblStudent).Column("StudentID");
    }
}
}

using System; 
using System.Collections.Generic; 
using System.Text; 
using FluentNHibernate.Mapping;

namespace human {


public class TblTermMap : ClassMap<TblTerm> {

    public TblTermMap() {
        Table("TblTerm");
        LazyLoad();
        Id(x => x.ID).GeneratedBy.Identity().Column("ID");
        References(x => x.TblLesson).Column("LessonID");
        References(x => x.TblStudent).Column("StudentID");
    }
}
}

 private void button_JoinSelect_Click(object sender, EventArgs e)
    {
        var cfg = new Configuration();
        PersistenceModel pm = new PersistenceModel();
        pm.AddMappingsFromAssembly(typeof(human.TblStudent).Assembly);
        pm.Configure(cfg);
        ISessionFactory factory = cfg.BuildSessionFactory();

        using (var session = factory.OpenSession())
        {
            using (var trans = session.BeginTransaction())
            {
                var query = session.QueryOver<TblStudent().JoinQueryOver<TblTerm>
      (c=> c.TblTerms).List();
                trans.Commit();
                dataGridView1.DataSource = query;
            }
        }
      }

我的错误:

NHibernate.Exceptions.GenericADOException: 无法执行查询 [ SELECT this_.ID as ID1_1_, this_.name as name1_1_, this_.lastname as lastname1_1_, tblterm1_.ID as ID2_0_, tblterm1_.LessonID as LessonID2_0_, tblterm1_.StudentID as StudentID2_0_ FROM TblStudent this_内部连接 ​​TblTerm tblterm1_ on this_.ID=tblterm1_.TblStudent_id ] [SQL: SELECT this_.ID as ID1_1_, this_.name as name1_1_, this_.lastname as lastname1_1_, tblterm1_.ID as ID2_0_, tblterm1_.LessonID as LessonID2_0_, tblterm1_.StudentID作为 StudentID2_0_ FROM TblStudent this_inner join TblTerm tblterm1_ on this_.ID=tblterm1_.TblStudent_id] ---> System.Data.SqlClient.SqlException:列名“TblStudent_id”无效。在 System.Data.SqlClient.SqlInternalConnection 的 System.Data.SqlClient.SqlConnection.OnError(SqlException 异常,布尔型 breakConnection)。1 querySpaces, IType[] resultTypes) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1595 at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Criteria\CriteriaLoader.cs:line 74 at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 1919 at NHibernate.Impl.CriteriaImpl.List(IList results) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 265 at NHibernate.Impl.CriteriaImpl.List[T]() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 276 at NHibernate.Impl.CriteriaImpl.Subcriteria.List[T]() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\CriteriaImpl.cs:line 813 at NHibernate.Criterion.QueryOver1.List() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Criterion\QueryOver.cs:第 57 行在 NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver.List() in d:\CSharp \NH\NH\nhibernate\src\NHibernate\Criterion\QueryOver.cs:第 198 行位于 human.Form1.button_JoinSelect_Click(Object sender, EventArgs e) in E:\desktop\Human\human\human\Form1.cs:line 84

请给我提意见

4

1 回答 1

0

该异常可能是多种原因造成的。例如,当我试图在我的数据库上运行你的代码而根本没有你的表时,我遇到了同样的错误。尝试检查您的任何表中的任何一条记录是否返回良好,然后尝试使您的加入工作。我没有看到错误,当我正确配置 nh 时,没有抛出异常。试试这个代码而不是你的工厂初始化(在新的新数据库上),它从你的映射创建表,看看你的真实表是否相同。

ISessionFactory factory = Fluently.Configure()
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TblStudent>())
            .ExposeConfiguration(config => new SchemaExport(config).Create(false, true));
于 2011-10-14T17:15:02.393 回答