Fluent Nhibernate: Can't create database connection to MySQL

时光毁灭记忆、已成空白 提交于 2020-01-15 09:41:31

问题


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? :)


回答1:


Does your schema exist? Because your using .ExposeConfiguration(BuildSchema) to "build" it?




回答2:


Unfortunatly I can´t tell you why but it seems to be an issue with the MySql connector.

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.




回答3:


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;


来源:https://stackoverflow.com/questions/1298527/fluent-nhibernate-cant-create-database-connection-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!