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;
}
}