
作用:用来方便连接C#与Mysql的中间件
用来方便连接C#与Mysql的中间件
https://nhibernate.info/
解决方案资源管理器—目标项目—引用—浏览—浏览
C:\Users\xxx\Desktop\NHibernate-5.2.7-bin\Required_Bins\NHibernate.dll
解决方案资源管理器—目标项目—右键—添加—新建项—XML文件
命名为:
hibernate.cfg.xml
点击一下—属性—高级—复制到输出目录—始终复制
<?xml version="1.0" encoding="utf-8" ?><hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"><session-factory><property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property><property name="dialect">NHibernate.Dialect.MySQL5Dialect</property><!--数据库版本--><property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property><!--使用什么数据库--><property name="connection.connection_string">Server=localhost;Database=mygamedb;User ID=root;Password=root;</property><!--数据库信息--><property name="show_sql">true</property></session-factory></hibernate-configuration>
改3处:数据库名字、用户名、密码
解决方案资源管理器—目标项目—右键—添加—新建项—类
NHibernateHelper.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NHibernate;using NHibernate.Cfg;namespace MyGameServer{class NHibernateHelper{private static ISessionFactory _sessionFactory;private static ISessionFactory SessionFactory{get{if (_sessionFactory == null){var configuration = new Configuration();configuration.Configure();configuration.AddAssembly("MyGameServer");//改成自己的名字_sessionFactory = configuration.BuildSessionFactory();}return _sessionFactory;}}public static ISession OpenSession() {return SessionFactory.OpenSession();}}}
改2处:namespace、configuration