2014-11-21 63 views
0

我想在MySql的NHibernate的一個簡單的項目,但我得到這個異常。 請幫助。 下面是代碼Nhibernate ArgumentNullException是未處理的值不能爲空參數名稱:stream

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和我打這個。

回答

相關問題