34

I'm trying to configure nHibernate to use a MySql database. I found examples for mssql and sqlite but none for mysql. So, how do I change this so it uses mysql:

Fluently.Configure().Database(
        MsSqlConfiguration.MsSql2005.ConnectionString(
            c => c.FromConnectionStringWithKey("ConnectionString")
        )
    )
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>())
    .BuildSessionFactory())
Spikolynn
  • 4,067
  • 2
  • 37
  • 44

2 Answers2

49

Change MsSqlConfiguration.MsSql2005, to MySqlConfiguration.Standard, it was the one thing I contributed to the project.

Example:

Fluently.Configure().Database(
        MySqlConfiguration.Standard.ConnectionString(
            c => c.FromConnectionStringWithKey("ConnectionString")
        )
    )
    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyAutofacModule>())
    .BuildSessionFactory())
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
  • Thank you, funny I missed this. – Spikolynn Mar 09 '09 at 14:08
  • 1
    signed in just to vote you guys up. now, can someone help me with nhibernate? i get error unable to create drive for mysql – jake Jan 18 '10 at 09:50
  • Cool, you might want to ask a whole new question with as much details as you can possibly give. I'm not familiar with that error off hand, so I can't give you much help without more details. I will say that the error sounds unusual, I don't think I've ever got that one and I mess around with Fluent NHib and MySql all the time. – Mark Rogers Jan 18 '10 at 15:12
  • Did you need to install any software for this to work? I'm new to NHibernate and MySQL... In particular, did you install MySql Connector, and if so what version? – Frank Schwieterman Apr 03 '10 at 23:41
  • 2
    Yes, I did need a driver from MySql Connector, specifically a reference to MySql.Data. I don't think the version is relevant as long as it's not an ancient one. I've never had a problem in terms of which version I was using. I don't think FluentNhibernate directly references the MySql.Data library, so there should not be a version mismatch. – Mark Rogers Apr 04 '10 at 03:26
  • @Mark Rogers I'm getting this error when I use your code. Is this an issue with my NHibernate version, or with MySQL? 'MySql.Data.MySqlClient.MySqlConfiguration' does not contain a definition for 'Standard' – Chazt3n Jan 29 '13 at 16:37
  • @Chazt3n - The MySql objects may have changed or something. But I think your connection string builder part needs to have something different where Standard is. If I had intellisense up I would do MySqlConfiguration. and look to see what was there. I think i've made a similar change in the past for a sql server connection. – Mark Rogers Jan 29 '13 at 16:57
  • 2
    I actually was able to solve the problem using MySQLcon... instead of MySql... it has had to have changed, because in every resource I see how you have it. Thank you for the response good sir. – Chazt3n Jan 29 '13 at 19:18
0
var SessionFactory = Fluently.Configure()
    .Database(MySQLConfiguration.Standard.ConnectionString(connectionString))
    .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
    .BuildSessionFactory();`

Try this Configuration.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
Mohit Arora
  • 337
  • 5
  • 19