簡體   English   中英

流利的Nhibernate:無法創建與MySQL的數據庫連接

[英]Fluent Nhibernate: Can't create database connection to MySQL

我一直在研究Fluent Nhibernate,這看起來確實很有希望。 但...

我似乎無法使其與MySQL數據庫一起使用。 我的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();

我得到一個奇怪的例外:

Value cannot be null. Parameter name: stream

我知道我可以使用同一項目中的簡單SQL語句連接到MySQL。

有任何想法嗎? :)

您的架構是否存在? 因為您使用.ExposeConfiguration(BuildSchema)來“構建”它?

我遇到了同樣的問題,最后我想通了。 首先,您需要使用示例中使用的表來設置數據庫。 請參見下面的腳本。 (我省略了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;

不幸的是,我無法告訴您原因,但這似乎與MySql連接器有關。

添加:

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

到您的配置,看看是否有幫助。 我在休眠論壇中找到了解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM