流利Nhibernate:不能创建到MySQL的数据库连接 [英] Fluent Nhibernate: Can't create database connection to MySQL
问题描述
我似乎无法使用MySQL数据库。我有Fluent Nhibernate示例项目与SQLite数据库运行正常( Fluent NHibernate:入门) ,但只要我将数据库配置更改为:
$ p $
return FULLntly.Configure()
.Database( MySQLConfiguration.Standard.ConnectionString(
x => x.Database(database)
.Server(server)
.Username(login)
.Password 密码)
)
)
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf< Program>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
我收到一个奇怪的异常:
值不能为空。参数名称:stream
我知道我可以通过一个简单的SQL语句连接到MySQL项目。
有什么想法? :)
我有同样的问题,最后我想通了。
首先,您需要使用示例中使用的表来设置数据库。见下面的脚本。 (我省略了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>()
;
$ b $返回流利.Configure()
.Database(cfg)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf< Program>())
.BuildSessionFactory()
;
创建BD和表格脚本:
DROP DATABASE IF EXESTTS testdb;
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE Employee
(
id int not null主键auto_increment,
名字文字,
姓名文字,
Store_id int(10)
);
CREATE TABLE产品
(
id int不是null主键auto_increment,
名称TEXT,
价格DOUBLE
);
CREATE TABLE存储
(
id int不为空主键auto_increment,
名称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员工;
SELECT'Product table'as'';
desc产品;
SELECT'存储表'为'';
desc商店;
SELECT'shopgroup_member table'as'';
desc StoreProduct;
I have been looking into Fluent Nhibernate, and it seems really promising. But...
I can't seem to get it working with a MySQL database. 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:
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.
Any ideas? :)
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).
Then I used the following CreateSessionFactory code:
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:
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;
这篇关于流利Nhibernate:不能创建到MySQL的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!