我正在使用MySql在NHibernate中尝试一个简单的项目但是我得到了这个异常。 请帮忙。 这是代码
class Cat
{
public string id { get; set; }
public string name { get; set; }
public string sex { get; set; }
public decimal weight { get; set; }
}
这是我的Cat.hbm.xml文件
<property name="name" type="String">
<column name="name" length="25" sql-type="varchar" not-null="true"/>
</property>
<property name="sex" column="sex" not-null="true" update="false"/>
<property name="weight" column="weight" not-null="true"/>
app.config文件
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Server=localhost;Database=mohit;User ID=root;Password=root</property>
<mapping assembly="Cats"/>
</session-factory>
</hibernate-configuration>
</configuration>
这是主要的program.cs
class Program
{
static void Main(string[] args)
{
Configuration cfg = new Configuration();
cfg.Configure();
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
ITransaction tx1 = session.BeginTransaction();
Cat c1 = new Cat();
c1.id = "cat1";
c1.name = "Fluffy";
c1.sex = "f";
c1.weight = new Decimal(3.2);
var c2 = new Cat();
c2.id = "cat2";
c2.name = "Mittens";
c2.sex = "m";
c2.weight = new Decimal(4.3);
session.Save(c1);
session.Save(c2);
tx1.Commit();
Console.WriteLine("successful");
ITransaction tx2 = session.BeginTransaction();
IList<Cat> cats = session.CreateQuery("FROM cats").List<Cat>();
foreach (Cat c in cats)
{
Console.WriteLine(c.name);
}
tx2.Commit();
session.Close();
}
请帮助我是Nhibernate的新手,我很震惊。
答案 0 :(得分:0)
似乎MySqlConnector中存在错误......
在web.config中添加该行
<property name="hbm2ddl.keywords">none</property>
您可以在以下链接中找到一些详细解决方案
Fluent Nhibernate: Can't create database connection to MySQL
答案 1 :(得分:0)
经过3天的挖掘,终于找到了解决方案。
将 NHibernate 属性 hbm2ddl.keywords 设置为无。 像下面 this._NHConfig.SetProperty("hbm2ddl.keywords", "none");
您也可以像上面的答案一样通过 web.config 来完成。