What did I do wrong here? I've installed the nhibernate, fluentnhibernate and System.Data.SqlClient nuget packages. I am new to nhibernate and ORM on general.
Here code and error:
public class Animal
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class AnimalMap : ClassMap<Animal>
{
public AnimalMap() {
Id(x => x.Id);
Map(x => x.Name);
Table("animals");
}
}
Demo.js
public class Demo
{
private static ISessionFactory _sessionFactor;
private static ISessionFactory SessionFactory{
get {
if(_sessionFactor == null) {
InitializeSessionFactory();
}
return _sessionFactor;
}
}
private static void InitializeSessionFactory () {
DbProviderFactories.RegisterFactory("System.Data.SqlClient", SqlClientFactory.Instance);
_sessionFactor = Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString(
"Server=localhost;Database=xxx;Uid=root;Pwd=666;"
))
.Mappings(mappings => mappings.FluentMappings
.AddFromAssemblyOf<Program>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession() {
return SessionFactory.OpenSession();
}
}
Program.cs
static void Main(string[] args)
{
using (var session = Demo.OpenSession()) {
using (var transaction = session.BeginTransaction()) {
var animal = new Animal {
Name = "Cat"
};
session.Save(animal);
transaction.Commit();
}
}
}
When I run this .net core console application, I get the error below
Unhandled exception. FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
---> FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
---> NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.MySqlDataDriver, NHibernate, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: The specified invariant name 'MySql.Data.MySqlClient' wasn't found in the list of registered .NET Data Providers. at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName, Boolean throwOnError) at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) at NHibernate.Driver.ReflectionBasedDriver..ctor(String providerInvariantName, String driverAssemblyName, String connectionTypeName, String commandTypeName) at NHibernate.Driver.MySqlDataDriver..ctor()
--- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& hasNoDefaultCtor) at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions) at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions) at System.Activator.CreateInstance(Type type) at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type) at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings)
--- End of inner exception stack trace --- at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary'2 settings) at NHibernate.Connection.ConnectionProvider.Configure(IDictionary'2 settings) at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary'2 settings) at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action'1 scriptAction, Boolean execute, Boolean justDrop, TextWriter exportOutput) at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action'1 scriptAction, Boolean execute, Boolean justDrop) at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean useStdOut, Boolean execute, Boolean justDrop) at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean useStdOut, Boolean execute) at nhibernatetutorial.Demo.<>c.b__3_1(Configuration cfg) in C:\Users\user\nhibernatetutorial\Demo.cs:line 31 at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() --- End of inner exception stack trace --- at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()--- End of inner exception stack trace --- at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() at nhibernatetutorial.Demo.InitializeSessionFactory() in C:\Users\user\nhibernatetutorial\Demo.cs:line 24 at nhibernatetutorial.Demo.get_SessionFactory() in C:\Users\user\nhibernatetutorial\Demo.cs:line 16 at nhibernatetutorial.Demo.OpenSession() in C:\Users\user\nhibernatetutorial\Demo.cs:line 37 at nhibernatetutorial.Program.Main(String[] args) in C:\Users\user\nhibernatetutorial\Program.cs:line 9