2009-01-11 17 views
9

क्या एनएचबीरनेट कॉन्फ़िगरेशन फ़ाइलों से डेटाबेस टेबल और सी # कक्षाएं उत्पन्न करना संभव है? इसके बाद, क्या कॉन्फ़िगरेशन फ़ाइलों को बदलना और टेबल और कॉन्फ़िगरेशन फ़ाइलों को गैर-विनाशकारी रूप से अपडेट करना संभव है?NHibernate कॉन्फ़िगरेशन फ़ाइलों से डेटाबेस जेनरेट करें

क्या आप ऐसा करने के लिए किसी भी उपकरण की सिफारिश करते हैं? (अधिमानतः मुफ्त ...)

उत्तर

13

जोआचिम द्वारा उल्लिखित अनुसार, यह "hbm2ddl.auto" सेटिंग है जिसे आप ढूंढ रहे हैं।

आप इस तरह कोड से सेट कर सकते हैं:

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

और आपका अपनी App.config फ़ाइल में सेट कर सकते हैं:

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

हां एनएचबीर्नेट कॉन्फ़िगरेशन फ़ाइलों से डेटाबेस टेबल और सी # कक्षाएं उत्पन्न करना संभव है। मुझे इस उदाहरण के साथ यहां समझाएं।

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> 

चरण 2: बस विन्यास फाइल को देखकर, आप वस्तु siply निम्नलिखित

तरह लग रहा है पता
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 
    } 
} 

चरण 3: और दोबारा यदि आप "कॉलम" नाम संपत्ति और उसके डेटा प्रकार को देखते हैं जो वास्तव में "पता" नामक वास्तविक बैकएंड डेटाबेस तालिका को संदर्भित करता है।

वहाँ भी उपकरणों की बड़ी राशि है कि (मदद आप इस तरह के यूएमएल के रूप में विभिन्न इनपुट, या वास्तविक डाटाबेस स्कीमा आदि

2

जांच "hbm2ddl.auto" सेटिंग के आधार पर आप के लिए सभी इन कलाकृतियों उत्पन्न करने के लिए कर रहे हैं कॉन्फ़िगरेशन या NHibernate.Cfg.Configuration)। "बनाएं" के साथ आप अपने मैपिंग से संपूर्ण डेटाबेस स्कीमा को फिर से बना सकते हैं और "अपडेट" सेटिंग को बस अपनी स्कीमा अपडेट करनी चाहिए।

संबंधित मुद्दे