[英]Fluent Nhibernate: Can't create database connection to MySQL
I have been looking into Fluent Nhibernate, and it seems really promising. 我一直在研究Fluent Nhibernate,这看起来确实很有希望。 But...
但...
I can't seem to get it working with a MySQL database. 我似乎无法使其与MySQL数据库一起使用。 I have the Fluent Nhibernate example project running fine with the SQLite database ( Fluent NHibernate: Getting Started ), but as soon as I change the database configuration to this:
我的Fluent Nhibernate示例项目在SQLite数据库( Fluent NHibernate:入门 )上运行良好,但是一旦我将数据库配置更改为:
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();
I get a strange exception: 我得到一个奇怪的例外:
Value cannot be null. Parameter name: stream
I know that I can connect to the MySQL with a simple SQL statement from the same project. 我知道我可以使用同一项目中的简单SQL语句连接到MySQL。
Any ideas? 有任何想法吗? :)
:)
Does your schema exist? 您的架构是否存在? Because your using
.ExposeConfiguration(BuildSchema)
to "build" it? 因为您使用
.ExposeConfiguration(BuildSchema)
来“构建”它?
I had the same problem and finally I figured it out. 我遇到了同样的问题,最后我想通了。 First you need to set a database with the tables used in the example.
首先,您需要使用示例中使用的表来设置数据库。 See script below.
请参见下面的脚本。 (I omitted the Location object).
(我省略了Location对象)。
Then I used the following CreateSessionFactory code: 然后,我使用了以下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()
;
Create BD and table script: 创建BD和表脚本:
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;
Unfortunatly I can´t tell you why but it seems to be an issue with the MySql connector. 不幸的是,我无法告诉您原因,但这似乎与MySql连接器有关。
Add this: 添加:
.ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"));
to your configuration and see if it helps. 到您的配置,看看是否有帮助。 I found the solution at the hibernate forums .
我在休眠论坛中找到了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.