Connect to the previous article using the Fluentnhibemate operations database, adding maps to the database
Http://www.cnblogs.com/fzxiaoyi/p/8443586.html
in the Model file to create a new class Nhibernatehelper
Write the following code:
Using nhibernate;using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;namespace mysqldatebase.model{//<summary>////This helper class is used to get session, use session to manipulate database/ </summary> class Nhibernatehelper {//Singleton mode sessionfactory can get session, use session can go to manipulate database. private static isessionfactory sessionfactory = null; private static string datebase = "Game"; Database name private static string dateusername = "root"; Database user name private static string Datepassword = "root"; Database Password private static isessionfactory Sessionfactory {get {if (Sessi Onfactory = = null) initsessionfanctory (); return sessionfactory; }}/Initialize sessionfactory private static void Initsessionfanctory () {/* exception occurred: Unhandled exception of type "FluentNHibernate.Cfg.FluentConfigurationException" in Workaround in FluentNHibernate.dll: Download Mysql-connector-net-6.9.9.msi for installation, use MySql.Data.dll connection http://dev.my sql.com/downloads/connector/net/*/sessionfactory = FLUENTNHIBERNATE.CFG.FLUENTLY.C Onfigure (). Database (FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString (db = db). Server ("localhost"). Database (datebase). Username (Dateusername). Password (Datepassword)). Mappings (x = x.fluentmappings.addfromassemblyof<nhibernatehelper> ()). Buildsessionfactory (); }//used to get session public static ISession Opensession () {return sessionfactory.opensession () ; } }}
2. Create the Nhibernatehelper help class to generate the Sessionfactory