流畅的NHibernate的MySQL配置

时间:2010-10-28 17:42:12

标签: mysql nhibernate fluent

我有MySQL的class-configurer流畅的nhibernate:

class MySqlInitializer : INHibernateInitializer
{
    public Configuration GetConfiguration()
    {
        var dbServer = "localhost";
        var dbUsername = "root";
        var dbName = "nhibernate";
        var dbPassword = "";
        var ormAssembly = "NHibernate_MySQL.Domain";

        var config = Fluently.Configure()
            .Database(MySQLConfiguration
                          .Standard
                          .ConnectionString(cs => cs
                                                      .Server(dbServer)
                                                      .Database(dbName)
                                                      .Username(dbUsername)
                                                      .Password(dbPassword)))
            .Mappings(
                x =>
                x.FluentMappings.AddFromAssembly(Assembly.Load(ormAssembly)));

        return config.BuildConfiguration();
    }
}

所有数据都是正确的。 所以,我也有集成测试(Xunit):

[Fact]
    public void GenerateMigrationScript()
    {
        var config = new MySqlInitializer().GetConfiguration();
        var factory = (ISessionFactoryImplementor) config.BuildSessionFactory();

        using (var session = factory.OpenSession())
        {
            var updateScripts = config
                .GenerateSchemaUpdateScript(factory.Dialect,
                                            new DatabaseMetadata((DbConnection) session.Connection, factory.Dialect));

            foreach (var updateScript in updateScripts)
            {
                Console.WriteLine(updateScript + ";");
            }
        }
    }

我的实体:

public interface IHiberEntutyBase
{
    Guid Id { get; set; }
}

public class HiberEntity : IHiberEntutyBase
{
    public virtual Guid Id { get; set; }

    public virtual string Name { get; set; }
}

class HiberEntityMap : ClassMap<HiberEntity>
{
    public HiberEntityMap()
    {
        Id(x => x.Id).GeneratedBy.Guid();

        Map(x => x.Name);
    }
}

当我运行GenerateMigrationScript()时写入“MySql.Data.MySqlClient.MySqlException:未知数据库'nhibernate'”。 请帮帮我。

1 个答案:

答案 0 :(得分:3)

它意味着它所说的:你没有一个名为“nhibernate”的数据库。

NHibernate不创建数据库,只创建模式(表,键)。数据库必须存在。