2012-05-02 31 views
7

Tôi đang học NHibernate nhưng không thành công. Tôi hiểu rằng các thông báo lỗi không chính xác.Lỗi lạ NHibernate

Vui lòng trợ giúp.

thông báo lỗi là

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

tập tin Class

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

mapping

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

config:

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

lớp chính

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); 
      } 

     } 

Trả lời

5

Như những người khác nói - bạn phải làm cho tài sản của bạn ảo. Nhưng điều này chỉ cần thiết nếu bạn muốn tổ chức của mình để có thể tải lười biếng, đọc lên trên đó đây http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx

Nếu bạn không muốn tải lười biếng bạn có thể vô hiệu hóa nó

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

Sau đó, bạn sẽ không cần ảo tính chất.

+0

Trong trường hợp bạn đang sử dụng Fluent NHibernate: Not.LazyLoad(); – leojh

2

Bạn cần khai báo tất cả thuộc tính Sản phẩm của mình là 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

Như thông báo lỗi nói, NHibernate yêu cầu các thuộc tính lớp thực thể được đánh dấu là 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

hiện hoạt động. nhưng "Sản phẩm" KHÔNG được lưu vào cơ sở dữ liệu. Tôi có tự tạo bảng "Sản phẩm" trong cơ sở dữ liệu không? – user595234

+1

Bạn có thể tự mình tạo hoặc cho phép NHibernate làm điều đó, tôi khuyên bạn nên đọc một chút tại đây http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx – Jon

+0

hướng dẫn đó không phải là trợ giúp. – user595234