数据并不是真正在nhibernate中存储数据库

时间:2011-09-10 13:30:45

标签: nhibernate

我正在使用nhibernate编写一个简单的程序 Program.cs的

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using NHibernate;
  using NHibernate.Cfg;
  using System.Data.SqlServerCe;
  using NHibernate.ByteCode.LinFu;


  namespace FirstSolution
  {
    class Program
  {
    static void Main(string[] args)
    {
        Product p = new Product();
        p.id = "padu";
        p.name = "Sayan";
        p.category = "Bogus";
        Configuration cfg = new Configuration();
        cfg.AddXmlFile("C:/Users/win7/Documents/Visual Studio 2008/Projects/FirstSolution/FirstSolution/Product.hbm.xml");
        try
        {
            using (ISessionFactory factory = cfg.BuildSessionFactory())
            {
                using (ISession session = factory.OpenSession())
                {
                    using (ITransaction transaction = session.BeginTransaction())
                    {

                        // Tell NHibernate that this object should be saved
                        session.Save(p);

                        // commit all of the changes to the DB and close the ISession
                        session.Flush();
                        transaction.Commit();
                        session.Close();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        string s;
        s = Console.ReadLine();

       }
       }
       }

的App.config

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <configSections>
       <section name="hibernate-configuration"
         type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
       </configSections>
      <connectionStrings>
      <add name="FirstSolution.Properties.Settings.Database1ConnectionString"
         connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|    \Database1.mdf;Integrated Security=True;User Instance=True"
       providerName="System.Data.SqlClient" />
      </connectionStrings>
      <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.SqlClientDriver</property>
    <property name="proxyfactory.factory_class">
         NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
     </property>
     <property name="connection.connection_string">Data Source=.\SQLEXPRESS;   AttachDBFilename=|DataDirectory|Database1.mdf;Integrated Security=true;User Instance=true;</property>
     <property name="show_sql">true</property>
     <mapping assembly="FirstSolution"/>
     </session-factory>
     </hibernate-configuration>
    </configuration>

Product.hbm.xml

   <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="FirstSolution"     assembly="FirstSolution">
    <class name="Product" table="UserTable" lazy="false">
    <id name="id" column="id" type="String" length="10">
    <generator class="assigned"/>
    </id>
    <property name="name" column="name" type="String" length="10"/>
   <property name="category" column="category" type="String" length="10"/>
   </class>
   </hibernate-mapping>

Product.cs

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;

   namespace FirstSolution
   {
    public class Product
    {
    public String id { get; set; }
    public String name { get; set; }
    public String category { get; set; }

    }
    }

程序正在运行,没有任何异常。生成的sql正在控制台上显示。但是没有数据保存在Database1.mdf的UserTable中

2 个答案:

答案 0 :(得分:1)

在构建应用程序并将数据保存在那里时,Database1.mdf正被复制到/ bin / Debug文件夹。

答案 1 :(得分:0)

嗯。我很惊讶您没有例外。 Product类必须具有虚拟属性,否则将无法工作。

另外,我看到你正在使用MsSqlCeDialect。我没有使用SqlExpress而是使用SqlExpress!= SqlCe。尝试使用MsSql2005Dialect或MsSql2005Dialect。