Unit Test of DataAccess of nhib.pdf using SQLite

Source: Internet
Author: User

 

To test the Controller, MVC will inevitably involve database access. If you access the same database every time, coupling will inevitably occur with this specific database, therefore, it is best to use the memory database SQLite for testing. At the same time, it is also conducive to continuous integration. A new database is generated for each test and discarded after the test is completed. The following configuration method uses fluentnhib.pdf, note thatThe Session for building Sechma must be consistent with the Session for data access.

 

 

Public class NHConfigurator
{
Public ISessionFactory _ sessionFactory;
Public static Configuration _ configuration;
Public ISession Session {get; set ;}

Public NHConfigurator ()
{
_ SessionFactory = CreateSessionFactory ();
Session = _ sessionFactory. OpenSession ();
BuildSchema (Session );
}

Private static void BuildSchema (ISession Session)
{
SchemaExport export = new SchemaExport (_ configuration );
Export. Execute (true, true, false, Session. Connection, null );

}

Public ISessionFactory CreateSessionFactory ()
{
If (_ sessionFactory = null)
{
_ SessionFactory = Fluently. Configure ()

. Database (
SQLiteConfiguration. Standard. InMemory (). ShowSql () // generate a statement
. Mappings (m => m. FluentMappings. AddFromAssemblyOf <Your_mapping_class> ())
. ExposeConfiguration (cfg => _ configuration = cfg
)
. BuildSessionFactory ();
}
Return _ sessionFactory;
}
}

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.