2012-05-02 42 views
7

我正在學習NHibernate,但失敗了。我覺得錯誤信息不準確。NHibernate奇怪的錯誤

請幫忙。

錯誤消息是

The following types may not be used as proxies: 
SecondSolution.Domain.Product: method get_Id should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method set_Id should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method get_Name should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method set_Name should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method get_Category should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method set_Category should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method get_Discontinued should be 'public/protected virtual' or 'protected internal virtual' 
SecondSolution.Domain.Product: method set_Discontinued should be 'public/protected virtual' or 'protected internal virtual' 
    at NHibernate.Cfg.Configuration.ValidateEntities() in c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\Cfg\Configuration.cs:line 
1052 
    at NHibernate.Cfg.Configuration.Validate() in c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\Cfg\Configuration.cs:line 959 
    at NHibernate.Cfg.Configuration.BuildSessionFactory() in c:\Users\oskar.berggren\Documents\Projects\nhibernate-core-3\src\NHibernate\Cfg\Configuration.cs:li 
ne 1251 
    at SecondSolution.Program.Main(String[] args) in C:\vs_workspace\SecondSolution\SecondSolution\Program.cs:line 22 
Press any key to continue . . . 

類文件

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

namespace SecondSolution.Domain 
{ 
    class Product 
    { 
     public Product() 
     { 
      this.Name = "John"; 
     } 
     public Guid Id { get; set; }   
     public string Name { get; set; }   
     public string Category { get; set; }   
     public bool Discontinued { get; set; } 
    } 
} 

映射

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"      
        assembly="SecondSolution"      
        namespace="SecondSolution.Domain"> 
    <class name="Product"> 
    <id name="Id"> 
     <generator class="guid" /> 
    </id> 
    <property name="Name" /> 
    <property name="Category" /> 
    <property name="Discontinued" /> 
    </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> 

主類

static void Main(string[] args) 
     { 
      try 
      { 
       Configuration cfg = new Configuration(); 
       cfg.Configure("Mappings/hibernate.cfg.xml"); 
       //cfg.Configure(); 

       cfg.AddAssembly(typeof(Product).Assembly); 

       NHibernate.ISessionFactory m_SessionFactory = cfg.BuildSessionFactory(); 
       NHibernate.ISession session = m_SessionFactory.OpenSession(); 
       Product product = new Product(); 
       session.SaveOrUpdate(product); 
      } catch (Exception e) 
      { 
       Console.WriteLine(e.Message); 
       Console.WriteLine(e.StackTrace); 
      } 

     } 

回答

5

當別人說 - 你必須讓你的財產是虛擬的。 但這個如果你想你的實體能夠延遲加載時才需要,這裏http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx

閱讀起來就可以了。如果你不希望延遲加載,你可以禁用它

<class name="Product" Lazy="false"> 

然後,你不會需要虛擬屬性。

+0

如果你使用流利的NHibernate: Not.LazyLoad(); – leojh

2

您需要將所有的產品性能聲明爲virtual

public virtual Guid Id { get; set; }   
public virtual string Name { get; set; }   
public virtual string Category { get; set; }   
public virtual bool Discontinued { get; set; } 
3

隨着錯誤消息指出,NHibernate的要求實體類的屬性被標記爲virtual

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

namespace SecondSolution.Domain 
{ 
    class Product 
    { 
     public Product() 
     { 
      this.Name = "John"; 
     } 
     public virtual Guid Id { get; set; }   
     public virtual string Name { get; set; }   
     public virtual string Category { get; set; }   
     public virtual bool Discontinued { get; set; } 
    } 
} 
+0

現在有效。但「產品」不保存到數據庫中。我是否在數據庫中手動創建「產品」表? – user595234

+1

你可以自己創建它,也可以讓NHibernate做到這一點,我建議在這裏閱讀一下http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx – Jon

+0

該教程不是幫助。 – user595234