【问题标题】:Fluent Nhibernate: Can't create database connection to MySQLFluent Nhibernate:无法创建与 MySQL 的数据库连接
【发布时间】:2009-08-19 08:32:17
【问题描述】:

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

我似乎无法让它与 MySQL 数据库一起使用。我的 Fluent Nhibernate 示例项目在 SQLite 数据库 (Fluent NHibernate: Getting Started) 上运行良好,但是一旦我将数据库配置更改为:

            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。

有什么想法吗? :)

【问题讨论】:

  • 请发布完整的异常,包括堆栈跟踪和内部异常 (ex.ToString())

标签: c# mysql nhibernate fluent-nhibernate


【解决方案1】:

很遗憾,我无法告诉您原因,但这似乎是 MySql 连接器的问题。

添加这个:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,它确实存在(我错过了那条线)。没有它,我会得到同样的错误。我尝试使用没有外键的单个表/对象映射使其更简单,但我仍然遇到相同的错误。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-27
      • 2014-03-06
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2014-08-06
      相关资源
      最近更新 更多