C# 流利的Nhibernate:Can';t创建到MySQL的数据库连接
我一直在研究Fluent Nhibernate,它看起来很有前途。但是 我似乎无法让它与MySQL数据库一起工作。我的Fluent Nhibernate示例项目在SQLite数据库()中运行良好,但一旦我将数据库配置更改为:C# 流利的Nhibernate:Can';t创建到MySQL的数据库连接,c#,mysql,nhibernate,fluent-nhibernate,C#,Mysql,Nhibernate,Fluent Nhibernate,我一直在研究Fluent Nhibernate,它看起来很有前途。但是 我似乎无法让它与MySQL数据库一起工作。我的Fluent Nhibernate示例项目在SQLite数据库()中运行良好,但一旦我将数据库配置更改为: return Fluently.Configure() .Database(MySQLConfiguration.Standard.ConnectionString( x => x.Dat
return Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(
x => x.Database("database")
.Server("server")
.Username("login")
.Password("password")
)
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
我知道我可以从同一个项目用一条简单的SQL语句连接到MySQL
有什么想法吗?:) 您的模式存在吗?因为您使用
.ExposeConfiguration(BuildSchema)
来“构建”它?不幸的是,我无法告诉您原因,但这似乎是MySql连接器的一个问题
添加以下内容:
.ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"));
查看您的配置是否有帮助。我在办公室找到了解决办法。我也遇到了同样的问题,最后我解决了。 首先,需要使用示例中使用的表设置数据库。请参阅下面的脚本。(我省略了Location对象) 然后我使用了以下CreateSessionFactory代码:
var cfg = MySQLConfiguration
.Standard
.ShowSql()
.UseOuterJoin()
.ConnectionString("Server=localhost;Port=3306;Database=testdb;Uid=USER;Password=PASSWORD;use procedure bodies=false;charset=utf8")
.Driver<NHibernate.Driver.MySqlDataDriver>()
;
return Fluently.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.BuildSessionFactory()
;
是的,它确实存在(我错过了那一行)。没有它,我也会犯同样的错误。我尝试简化它,使用没有外键的单表/对象映射,但仍然得到相同的错误。请发布完整异常,包括堆栈跟踪和内部异常(
ex.ToString()
)
var cfg = MySQLConfiguration
.Standard
.ShowSql()
.UseOuterJoin()
.ConnectionString("Server=localhost;Port=3306;Database=testdb;Uid=USER;Password=PASSWORD;use procedure bodies=false;charset=utf8")
.Driver<NHibernate.Driver.MySqlDataDriver>()
;
return Fluently.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.BuildSessionFactory()
;
DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE Employee
(
id int not null primary key auto_increment,
FirstName TEXT,
LastName TEXT,
Store_id int(10)
);
CREATE TABLE Product
(
id int not null primary key auto_increment,
Name TEXT,
Price DOUBLE
);
CREATE TABLE Store
(
id int not null primary key auto_increment,
Name TEXT
);
CREATE TABLE StoreProduct
(
Store_id int(10) DEFAULT '0' NOT NULL,
Product_id int(10) DEFAULT '0' not null,
PRIMARY KEY (Store_id, Product_id)
);
SELECT 'Employee table' as '';
desc Employee;
SELECT 'Product table' as '';
desc Product;
SELECT 'Store table' as '';
desc Store;
SELECT 'shopgroup_member table' as '';
desc StoreProduct;