Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all

this is my code

i am having error like this,

'NHibernate.ISessionFactory' does not contain a definition for 'ConnectionProvider' and no extension method 'ConnectionProvider' accepting a first argument of type 'NHibernate.ISessionFactory' could be found (are you missing a using directive or an assembly reference?)


C#
public void ClearDatabase()
        {
            // Initialize
            SqlConnection connection = m_SessionFactory.ConnectionProvider.GetConnection() as SqlConnection;
            SqlCommand command = null;
            string[] dataTables = new string[] { "OrderItems", "Products", "Orders", "Customers" };
            string sql = null;

            // Delete all records from all tables
            using (connection)
            {
                // Iterate tables
                for (int i = 0; i < dataTables.Length; i++)
                {
                    // Build query and command, and execute
                    sql = String.Format("Delete from {0}", dataTables[i]);
                    command = new SqlCommand(sql, connection);
                    command.ExecuteNonQuery();
                }
            }
        }
Posted
Updated 22-Jun-14 23:56pm
v2
Comments
Prasad Avunoori 23-Jun-14 6:10am    
The error itself says that "ConnectionProvider" not existing in m_SessionFactory.

As quoted in the URL[^]

"You will also need to cast the SessionFactory to the concrete implementation (NHibernate.Impl.SessionFactoryImpl) in order to access the ConnectionProvider property (at least in NH v3)"
 
Share this answer
 
If you using multiple database then you have to give seperate session-factory.
your error says that there is no connection provider.

so, just check your nhibernate.cfg.xml (for availability of connection provider)
refer below config file code (and change config file according to that).
XML
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2-x-factories">

    <session-factory name="Development">
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Server=Sql01;DataBase=dbABC1;uid=A;pwd=A123</property>

        <property name="show_sql">true</property>

        <mapping assembly="DataLayer" />
    </session-factory>

    <session-factory name="Production">
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">Server=Sql02;DataBase=dbABC2;uid=A1;pwd=A1234</property>

        <property name="show_sql">false</property>

        <mapping assembly="DataLayer" />
    </session-factory>

</hibernate-configuration>
 
Share this answer
 
Are you using dependency injection? Please check if you have configured it correctly in your config file.
Refer http://www.nhforge.org/doc/nh/en/#session-configuration
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900