我正在为数据库编程连接到网络核心的api。我决定禁止但我无法连接到mysql数据库,这是一个本地配置,我有以下内容
那是我的CreateSessionFactory
private static ISessionFactory CreateSessionFactory(){try {
string connection = "server='localhost';Database='equalsbd';UserId='root';Password='123456';port=3304";
var db = MySQLConfiguration.Standard.ConnectionString(connection);
return Fluently.Configure().Database(db)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf
<SessionNHibernate>()).BuildSessionFactory();
}
catch
{
}
return null;
}
这是我的全班
public class Session NHibernate {
private static ISessionFactory sessionFactory = null;
private static ISession session = null;
private static ISessionFactory SessionFactory
{
get
{
if (SessionFactory == null)
sessionFactory = CreateSessionFactory();
return sessionFactory;
}
}
public static ISession Session
{
get { return session; }
}
public static ISession OpenSession() {
try
{
session = SessionFactory.OpenSession();
return session;
}
catch (Exception ex)
{
var p = ex;
return session;
}
}
public static void CloseSession()
{
session.Close();
}
private static ISessionFactory CreateSessionFactory()
{
try
{
string connection = "server='localhost';Database='equalsbd';UserId='root';Password='123456';port=3304";
var db = MySQLConfiguration.Standard.ConnectionString(connection);
return Fluently.Configure().Database(db)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf
<SessionNHibernate>()).BuildSessionFactory();
}
catch
{
}
return null;
}
}
我做的第一个改变是修改
private static ISessionFactory sessionFactory = null;
通过
private static ISessionFactory _sessionFactory = null;
防止混淆
第二个是通过将其私有属性更改为protected来更新usermap,这是私有的,由于某种原因不允许连接读取属性
以这种方式离开
这个
public class Usuarios
{
public virtual long idUsuarios { get; private set; }
public virtual string Usuario { get; private set; }
public virtual string Password { get; private set; }
public virtual string Perfiles_idPerfiles { get; private set; }
public static List<Usuarios> ListaUsuario() {
using (var session = SessionNHibernate.OpenSession() )
{
return session.Query<Usuarios>().ToList();
}
}
}
以正确的方式
public class Usuarios
{
public virtual long idUsuarios { get; protected set; }
public virtual string Usuario { get; protected set; }
public virtual string Password { get; protected set; }
public virtual string Perfiles_idPerfiles { get; protected set; }
public static List<Usuarios> ListaUsuario() {
using (var session = SessionNHibernate.OpenSession() )
{
return session.Query<Usuarios>().ToList();
}
}
}
有了这个,您与mysql的连接将起作用或与任何其他数据库一起使用