Iam new to nhibernate . I was just trying to execute an Sp using nhibernate. I have compiled the solution by making the below contact.cs as embedded resource.
But its giving me the error could not execute query [ exec [Nhibernate-Test-Sp] ] [SQL: exec [Nhibernate-Test-Sp]]
Please help me out
Code to execute the query
public List<Contact> GetContacts() { ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory(); NHibernate.ISession session = sessionFactory.OpenSession(); IQuery query = session.GetNamedQuery("GetContacts"); return query.List<Contact>() as List<Contact>; }
This is my Contact.cs class
public class Contact { private int _contactId; private string _firstName; private string _lastName; private string _email; private string _telephone; private static int id = 10;
public Contact(int contactid, string firstname, string lastname, string email, string telephone) { _contactId = contactid; _firstName = firstname; _lastName=lastname; _email = email; _telephone = telephone; }
public Contact() { } public virtual int ContactId { get { return _contactId; } set { _contactId = value; } }
public virtual string FirstName { get { return _firstName; } set { _firstName = value; } }
public virtual string LastName { get { return _lastName; } set { _lastName = value; } }
public virtual string Email { get { return _email; } set { _email = value; } }
public virtual string Telephone { get { return _telephone; } set { _telephone = value; } }
}
Mapping Xml for the class
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <sql-query name="GetContacts"> <return-scalar column="ContactId" type="Int" /> <return-scalar column="FirstName" type="String" /> <return-scalar column="LastName" type="String" /> <return-scalar column="Email" type="String" /> <return-scalar column="Telephone" type="String" /> exec [Nhibernate-Test-Sp] </sql-query> </hibernate-mapping>
This is my procedure
CREATE PROCEDURE [DEV\tcs-nby].[Nhibernate-Test-Sp] AS BEGIN SET NOCOUNT ON; SELECT Contact_id,[first_name],last_name,email,telephone FROM[tbl_contact] END GO
Web.config Details
<configuration> <configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> </configSections> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <reflection-optimizer use="false" /> <session-factory> <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> <property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> <property name="connection.connection_string">Server=VICDEVD;database=DEV121;Integrated Security=SSPI;</property> <property name="show_sql">false</property> <property name="connection.driver_class"> NHibernate.Driver.SqlClientDriver </property> <property name="connection.isolation">ReadCommitted</property> <mapping assembly="Contacts" />
</session-factory> </hibernate-configuration> Thanks
Renjith
|