使用queryover连接2表

时间:2011-10-14 12:10:00

标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

我在样本中有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:无法执行查询 [选择this_.ID为ID1_1_,this_.name为name1_1_,this_.lastname为lastname1_1_,tblterm1_.ID为ID2_0_,tblterm1_.LessonID为LessonID2_0_,tblterm1_.StudentID为StudentID2_0_ FROM TblStudent this_ inner join TblTerm tblterm1_ on this_.ID = tblterm1_ .TblStudent_id] [SQL:选择this_.ID为ID1_1_,this_.name为name1_1_,this_.lastname为lastname1_1_,tblterm1_.ID为ID2_0_,tblterm1_.LessonID为LessonID2_0_,tblterm1_.StudentID为StudentID2_0_ FROM TblStudent this_ inner join TblTerm tblterm1_ on this_.ID = tblterm1_.TblStudent_id] ---&gt; System.Data.SqlClient.SqlException:无效的列名称'TblStudent_id'。    在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection)    在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)    在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()    在System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)    在System.Data.SqlClient.SqlDataReader.ConsumeMetaData()    在System.Data.SqlClient.SqlDataReader.get_MetaData()    在System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)    在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async)    在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,DbAsyncResult result)    在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method)    在System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String method)    在System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行为)    在System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()    at NHibernate.AdoNet.AbstractBatcher.ExecuteReader(IDbCommand cmd)位于d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ AdoNet \ AbstractBatcher.cs:第247行    at NHibernate.Loader.Loader.GetResultSet(IDbCommand st,Boolean autoDiscoverTypes,Boolean callable,RowSelection selection,ISessionImplementor session)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:第1349行    at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:第413行    at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session,QueryParameters queryParameters,Boolean returnProxies)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:第243行    at NHibernate.Loader.Loader.DoList(ISessionImplementor session,QueryParameters queryParameters)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:1694行    ---内部异常堆栈跟踪结束---    at NHibernate.Loader.Loader.DoList(ISessionImplementor session,QueryParameters queryParameters)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:第1703行    at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session,QueryParameters queryParameters)在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Loader \ Loader.cs:第1601行    在NHibernate.Loader.Loader.List(ISessionImplementor session,QueryParameters queryParameters,ISet 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.QueryOver 1.List()在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Criterion \ QueryOver.cs:第57行    at NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver.List()在d:\ CSharp \ NH \ NH \ nhibernate \ src \ NHibernate \ Criterion \ QueryOver.cs:第198行    at Human.Form1.button_JoinSelect_Click(Object sender,EventArgs e)在E:\ desktop \ Human \ human \ human \ Form1.cs:第84行

请告诉我

1 个答案:

答案 0 :(得分:0)

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

ISessionFactory factory = Fluently.Configure()
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<TblStudent>())
            .ExposeConfiguration(config => new SchemaExport(config).Create(false, true));