当前位置:编程学习 > C#/ASP.NET >>

NHibernate 新手 肯定给分

一个新手, 写了一个 NHibernate 的程序,以前用过java的Hibernate, 突然感觉两个很想色,但是不一样

写了一个类,就是想把这个类插到MySQL的数据库里面,帮忙看看,怎么做呢

下面是程序的截图, 总是弹出来说让我找一个源代码, NHinernate 和 Iesi 都加进去了



这个是真个项目的结构图, NHinernate 和 Iesi 都加进去了, 怎么还要代码



相关的代码, 一个类, 一个映射文件*.hbm.xml, 一个hbm的配置文件, 还有就是主程序了


namespace Thiess.Data
{
        public class Product
        {
            public string m_ID { get; set; }
            public string m_Name { get; set; }
        }

}




static void Main(string[] args)
        {
            Product[] cs = new Product[MaxRow];
            for (int i = 1; i <= MaxRow; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("用户");
                sb.Append(i);

                Product c = new Product();
                c.m_ID = i.ToString();
                c.m_Name = sb.ToString();


                cs[i - 1] = c;
            }

            Console.WriteLine("=================== TEST START ===================");




            Configuration cfg = new Configuration();

            ISession session = cfg.BuildSessionFactory().OpenSession();
            ITransaction transaction = session.BeginTransaction();


            session.Save(cs[0]);
            transaction.Commit();
            session.Close();
       }



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Thiess"
                   namespace="Thiess.Data">

  <!-- more mapping info here -->
  <class name="Product" table="Test_product_table" lazy="false">
    <id name="m_ID" column="Product_ID" type="string">
      <generator class="native" />
    </id>
    <property name="m_Name" column="Product_Name" type="string" length="20"/>
  </class>

</hibernate-mapping>




<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source=FirstSample.sdf</property>

    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>
--------------------编程问答-------------------- 感觉像是程序报错,要你调试源代码

你用Nuget 引用NHinernate 和 Iesi 试试
  
*****************************************************************************
http://feiyun0112.cnblogs.com/ --------------------编程问答-------------------- 试试
            
  ...
            Configuration cfg = new Configuration();

            cfg.Configure();
            cfg.AddAssembly(typeof(Thiess.Data.Product).Assembly);

            ISession session = cfg.BuildSessionFactory().OpenSession();
  ...

另外,你用的是SqlServerCeDriver --------------------编程问答-------------------- 配置文件有问题啊!那么明显,你要连接的是MYSQL ,而数据库又是sdf?driver_class和dialect。你看下你

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <!-- ISessionFactory实例 -->
   <session-factory>
     <!-- 属性 -->
     <!-- 设置connection provider,NHibernate将用之连接数据库 -->
     <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <!-- 设置所用driver,在此,我们为MySQL数据库选择MySqlDataDriver -->
     <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
     <!-- 设置连接到数据库的connection string -->
     <property name="connection.connection_string">***********</property>
     <!-- 为特定数据库选择Dialect,这里选择使用MySQL5Dialect -->
     <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
     <!-- 映射文件 -->
     <mapping resource="SimpleNHibernateTour.Employee.hbm.xml" assembly="SimpleNHibernateTour" />
   </session-factory>
 </hibernate-configuration>
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,