Fluent Nhibernate:无法创建与MySQL的数据库连接

时间:2009-08-19 08:32:18

标签: c# mysql nhibernate fluent-nhibernate

我一直在研究Fluent Nhibernate,看起来很有希望。但...

我似乎无法使用MySQL数据库。我使用SQLite数据库(Fluent NHibernate: Getting Started)正常运行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();

我得到一个奇怪的例外:

Value cannot be null. Parameter name: stream

我知道我可以使用来自同一项目的简单SQL语句连接到MySQL。

有什么想法吗? :)

3 个答案:

答案 0 :(得分:0)

您的架构是否存在?因为您使用.ExposeConfiguration(BuildSchema)来构建它吗?

答案 1 :(得分:0)

不幸的是,我无法告诉你原因,但似乎是MySql连接器的一个问题。

添加:

.ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"));

到您的配置,看看它是否有帮助。我在hibernate forums找到了解决方案。

答案 2 :(得分:0)

我有同样的问题,最后我想出来了。 首先,您需要使用示例中使用的表来设置数据库。见下面的脚本。 (我省略了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()
             ;

创建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;