我该如何修复 NHibernate.Cfg.HibernateConfigException
how can I fix NHibernate.Cfg.HibernateConfigException
我在做一个应用nebernate的例子时发现了一个错误,但我不知道是什么问题。看这里的代码,找到你要修改的部分。
- 我的数据库是 mysql,我更改了 cfg.xml、him.xml
的属性
- 错误:NHibernate.Cfg.HibernateConfigException:'解析配置时发生异常
MainWindow.cs
private void GetStudent()
{
System.Collections.IList siteList;
ISessionFactory factory =
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
using (ISession session = factory.OpenSession())
{
ICriteria sc = session.CreateCriteria(typeof(Student));
siteList = sc.List();
session.Close();
}
factory.Close();
datagrid.ItemsSource = siteList;
}
Stduent.cs
public class Student
{
private int grade;
private int cclass;
private string name;
private int no;
private string score;
public virtual int Grade {
get { return grade; }
set { grade = value; }
}
public virtual int Cclass
{
get { return cclass; }
set { cclass = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual int No
{
get { return no; }
set { no = value; }
}
public virtual string Score
{
get { return score; }
set { score = value; }
}
public Student()
{
}
}
Student.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="WpfStudent.Student" dynamic-insert="true" dynamic-update="true" table="student">
<id name="No" column="no" type="int">
<generator class="increment"></generator>
</id>
<property name="Grade" column="grade" type="int"></property>
<property name="Cclass" column="cclass" type="int"></property>
<property name="Name" column="name" type="String"></property>
<property name="Score" column="score" type="String"></property>
</class>
</hibernate-mapping>
休眠。cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<!-- an ISessionFactory instance -->
<session-factory>
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">server=localhost;user id=root;persistsecurityinfo=True;database=student;allowuservariables=True</property>
<property name="dialect">NHibernate.Dialect.MySQL55InnoDBDialect</property>
<!-- mapping files -->
<mapping resource="WpfStudent.Student.hbm.xml" assembly="WpfStudent" />
</session-factory>
</hibernate-configuration>
MainWindow.xaml
<Window x:Class="WpfStudent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfStudent"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel LastChildFill="False">
<Border DockPanel.Dock="Left" Width="610" Padding="10">
<DataGrid Width="590" Height="400" HorizontalAlignment="Left" Name="studentDataGrid" ColumnWidth="*" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="grade" Binding="{Binding grade,NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="class" Binding="{Binding cclass, NotifyOnTargetUpdated=True,UpdateSourceTrigger=PropertyChanged}" Foreground="Black"/>
<DataGridTextColumn Header="name" Binding="{Binding name, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="no" Binding="{Binding no, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="score" Binding="{Binding score, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
</Border>
<Border DockPanel.Dock="Right" Width="180" Padding="0">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="35*"/>
<ColumnDefinition Width="15*"/>
</Grid.ColumnDefinitions>
<Button x:Name="ReadBtn" Content="READ" VerticalAlignment="Center" Height="25" Margin="10,15,10,15" Grid.Row="0" Grid.Column="1" Click="ReadBtn_Click"/>
<Button x:Name="InsertBtn" Content="INSERT" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="1" Grid.Column="1" Click="InsertBtn_Click"/>
<Button x:Name="UpdateBtn" Content="UPDATE" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="2" Grid.Column="1" Click="UpdateBtn_Click"/>
<Button x:Name="DeleteBtn" Content="DELETE" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="3" Grid.Column="1" Click="DeleteBtn_Click" />
</Grid>
</Border>
</DockPanel>
</Window>
name属性value(最后位置)。而不是这个:
<property name ="connection.provider ">...
<property name ="dialect ">...
<property name ="connection.driver_class ">...
<property name ="connection.connection_string ">..
我们需要:
<property name ="connection.provider">...
<property name ="dialect">...
<property name ="connection.driver_class">...
<property name ="connection.connection_string">..
NOTE: also, in VS, add xml schema into play "nhibernate-configuration.xsd" and for mapping files "nhibernate-mapping.xsd" ... that will reveal lot of issues even before compilation
因为你的数据库是 MySql 你应该使用
<property name="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</property>
<property name="dialect">
NHibernate.Dialect.MySQL55InnoDBDialect
</property>
参见 documentation Table 3.3。 NHibernate SQL Dialects 用于 MySql 的可能方言。
另外 Data Source=
在你的 connection.connection_string
看起来很奇怪。我会删除这个字符串。
如果尚未完成,您需要在项目中安装和引用 Nuget 包 MySql.Data。
我在做一个应用nebernate的例子时发现了一个错误,但我不知道是什么问题。看这里的代码,找到你要修改的部分。
- 我的数据库是 mysql,我更改了 cfg.xml、him.xml 的属性
- 错误:NHibernate.Cfg.HibernateConfigException:'解析配置时发生异常
MainWindow.cs
private void GetStudent()
{
System.Collections.IList siteList;
ISessionFactory factory =
new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
using (ISession session = factory.OpenSession())
{
ICriteria sc = session.CreateCriteria(typeof(Student));
siteList = sc.List();
session.Close();
}
factory.Close();
datagrid.ItemsSource = siteList;
}
Stduent.cs
public class Student
{
private int grade;
private int cclass;
private string name;
private int no;
private string score;
public virtual int Grade {
get { return grade; }
set { grade = value; }
}
public virtual int Cclass
{
get { return cclass; }
set { cclass = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
public virtual int No
{
get { return no; }
set { no = value; }
}
public virtual string Score
{
get { return score; }
set { score = value; }
}
public Student()
{
}
}
Student.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="WpfStudent.Student" dynamic-insert="true" dynamic-update="true" table="student">
<id name="No" column="no" type="int">
<generator class="increment"></generator>
</id>
<property name="Grade" column="grade" type="int"></property>
<property name="Cclass" column="cclass" type="int"></property>
<property name="Name" column="name" type="String"></property>
<property name="Score" column="score" type="String"></property>
</class>
</hibernate-mapping>
休眠。cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<!-- an ISessionFactory instance -->
<session-factory>
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">server=localhost;user id=root;persistsecurityinfo=True;database=student;allowuservariables=True</property>
<property name="dialect">NHibernate.Dialect.MySQL55InnoDBDialect</property>
<!-- mapping files -->
<mapping resource="WpfStudent.Student.hbm.xml" assembly="WpfStudent" />
</session-factory>
</hibernate-configuration>
MainWindow.xaml
<Window x:Class="WpfStudent.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfStudent"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel LastChildFill="False">
<Border DockPanel.Dock="Left" Width="610" Padding="10">
<DataGrid Width="590" Height="400" HorizontalAlignment="Left" Name="studentDataGrid" ColumnWidth="*" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="grade" Binding="{Binding grade,NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="class" Binding="{Binding cclass, NotifyOnTargetUpdated=True,UpdateSourceTrigger=PropertyChanged}" Foreground="Black"/>
<DataGridTextColumn Header="name" Binding="{Binding name, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="no" Binding="{Binding no, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="score" Binding="{Binding score, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
</Border>
<Border DockPanel.Dock="Right" Width="180" Padding="0">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
<RowDefinition Height="25*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="35*"/>
<ColumnDefinition Width="15*"/>
</Grid.ColumnDefinitions>
<Button x:Name="ReadBtn" Content="READ" VerticalAlignment="Center" Height="25" Margin="10,15,10,15" Grid.Row="0" Grid.Column="1" Click="ReadBtn_Click"/>
<Button x:Name="InsertBtn" Content="INSERT" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="1" Grid.Column="1" Click="InsertBtn_Click"/>
<Button x:Name="UpdateBtn" Content="UPDATE" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="2" Grid.Column="1" Click="UpdateBtn_Click"/>
<Button x:Name="DeleteBtn" Content="DELETE" VerticalAlignment="Center" Height="25" Margin="10" Grid.Row="3" Grid.Column="1" Click="DeleteBtn_Click" />
</Grid>
</Border>
</DockPanel>
</Window>
name属性value(最后位置)。而不是这个:
<property name ="connection.provider ">...
<property name ="dialect ">...
<property name ="connection.driver_class ">...
<property name ="connection.connection_string ">..
我们需要:
<property name ="connection.provider">...
<property name ="dialect">...
<property name ="connection.driver_class">...
<property name ="connection.connection_string">..
NOTE: also, in VS, add xml schema into play "nhibernate-configuration.xsd" and for mapping files "nhibernate-mapping.xsd" ... that will reveal lot of issues even before compilation
因为你的数据库是 MySql 你应该使用
<property name="connection.driver_class">
NHibernate.Driver.MySqlDataDriver
</property>
<property name="dialect">
NHibernate.Dialect.MySQL55InnoDBDialect
</property>
参见 documentation Table 3.3。 NHibernate SQL Dialects 用于 MySql 的可能方言。
另外 Data Source=
在你的 connection.connection_string
看起来很奇怪。我会删除这个字符串。
如果尚未完成,您需要在项目中安装和引用 Nuget 包 MySql.Data。