2009-01-11 9 views

Odpowiedz

13

Jak wspomniano przez Joachima, jest to ustawienie "hbm2ddl.auto", którego szukasz.

Można ustawić go za pomocą kodu tak:

var cfg = new NHibernate.Cfg.Configuration(); 
cfg.SetProperty("hbm2ddl.auto", "create"); 
cfg.Configure(); 

I można również ustawić go w pliku app.config:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="hbm2ddl.auto">create</property> 
2

Tak, możliwe jest generowanie tabel bazy danych i klas C# z plików konfiguracyjnych NHibernate. Pozwól mi wyjaśnić na tym przykładzie tutaj.

1: Address.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 

<hibernate-mapping 
    xmlns="urn:nhibernate-mapping-2.0" 
    default-cascade="none"> 

    <class 
     name="Your.Domain.AddressImpl, Your.Domain" 
     table="[ADDRESS]" 
     dynamic-insert="true" 
     dynamic-update="true" 
     lazy="true"> 

     <id name="Id" type="long" unsaved-value="0"> 
      <column name="ID" sql-type="NUMERIC(19,0)"/> 
      <generator class="native">   </generator> 
     </id> 



     <property name="Address1" type="string"> 
      <column name="ADDRESS1" not-null="true" unique="false" sql-type="VARCHAR(100)"/> 
     </property> 

     <property name="Address2" type="string"> 
      <column name="ADDRESS2" not-null="true" unique="false" sql-type="VARCHAR(100)"/> 
     </property> 

     <property name="City" type="string"> 
      <column name="CITY" not-null="true" unique="false" sql-type="VARCHAR(100)"/> 
     </property> 

     <property name="State" type="string"> 
      <column name="STATE" not-null="true" unique="false" sql-type="VARCHAR(100)"/> 
     </property> 

     <property name="Zipcode" type="string"> 
      <column name="ZIPCODE" not-null="true" unique="false" sql-type="VARCHAR(100)"/> 
     </property> 




    </class> 
</hibernate-mapping> 

Krok 2: Tylko przez patrząc na pliku konfiguracyjnego, to adres obiektu siply wygląda następującym

using System; 

namespace Your.Domain 
{ 

    public partial class Address 
    { 



     #region Attributes and Associations 

     private string _address1; 
     private string _address2; 
     private string _city; 
     private long _id; 
     private string _state; 
     private string _zipcode; 

     #endregion 

     #region Properties 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual string Address1 
     { 
      get { return _address1; } 
      set { this._address1 = value; } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual string Address2 
     { 
      get { return _address2; } 
      set { this._address2 = value; } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual string City 
     { 
      get { return _city; } 
      set { this._city = value; } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual long Id 
     { 
      get { return _id; } 
      set { this._id = value; } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual string State 
     { 
      get { return _state; } 
      set { this._state = value; } 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     public virtual string Zipcode 
     { 
      get { return _zipcode; } 
      set { this._zipcode = value; } 
     } 


     #endregion 
    } 
} 

Krok 3: A ponownie, jeśli spojrzysz na właściwość nazwy "Kolumny" i jej typ danych, który faktycznie odnosi się do rzeczywistej tabeli bazy danych backend o nazwie "Adres".

Istnieje również ogromna ilość narzędzi, które pomogą Ci wygenerować wszystkie te artefakty dla Ciebie w oparciu o różne wejścia, takie jak UML, lub rzeczywistego schematu bazy danych itp

2

Sprawdź ustawienie „hbm2ddl.auto” (w konfiguracja lub NHibernate.Cfg.Configuration). z "create" możesz odtworzyć cały schemat bazy danych z mapowań, a ustawienie "update" powinno po prostu zaktualizować twój schemat.

Powiązane problemy