C# 如何修复NHibernate.Cfg.hibernateConfigeException

C# 如何修复NHibernate.Cfg.hibernateConfigeException,c#,mysql,nhibernate,C#,Mysql,Nhibernate,我在举例应用nebernate时发现了一个错误,但我不知道问题出在哪里。查看此处的代码,找到要修改的部件 我的数据库是mysql,我更改了cfg.xml、him.xml的属性 错误:NHibernate.Cfg.hibernateConfigeException:“分析配置时发生异常 MainWindow.cs private void GetStudent() { System.Collections.IList siteList; IS

我在举例应用nebernate时发现了一个错误,但我不知道问题出在哪里。查看此处的代码,找到要修改的部件

  • 我的数据库是mysql,我更改了cfg.xml、him.xml的属性
  • 错误:NHibernate.Cfg.hibernateConfigeException:“分析配置时发生异常
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;
    }
 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()
    {

    }
}
Stduent.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;
    }
 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>
  <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>
<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>

hibernate.cfg.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>
  <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>
<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>

NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.MySqlDataDriver
服务器=本地主机;用户id=根用户;persistsecurityinfo=True;数据库=学生;allowuservariables=True
NHibernate.dialogue.mysql55innodbdialogue
main window.xaml

    <?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>
  <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>
<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>

名称atribute值(最后位置)处存在冗余空间。与此相反:

<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">..
。。。
...
...
..
注意:另外,在VS中,将xml模式添加到“nhibernate configuration.xsd”和映射文件“nhibernate mapping.xsd”中。。。这将揭示许多问题,甚至在汇编之前


因为您的数据库是MySql,所以您可能应该使用

<property name="connection.driver_class">
  NHibernate.Driver.MySqlDataDriver
</property>
<property name="dialect">
  NHibernate.Dialect.MySQL55InnoDBDialect
</property>

NHibernate.Driver.MySqlDataDriver
NHibernate.dialogue.mysql55innodbdialogue
见表3.3。NHibernate SQL方言,了解MySql可能使用的方言。 另外,
Data Source=
在您的
连接中。连接字符串对我来说很奇怪。我会把这根绳子拔下来


如果还没有完成,您需要在项目中安装并引用Nuget包MySql.Data。

很乐意帮助您进行配置。。。