2013-01-10 26 views
2

我希望有人外鍵以前碰到這個問題,可以幫助我out.In我的數據庫,我使用的是表關係插入行中的一個子表基於使用NHibernate

之間
Ex:Authors,Author Books,Booksstore(salesDetails) 
**Authors table** 
================= 
Aid int (Identity and Primarykey),AuthorName varchar(50) 
================= 

*********** 
**Author Books table** 
================= 
BookID int(primary key),BookName nvarchar(50),Aid int (foreign key Authorstable(Aid)) 
================= 

************** 
**Booksstore table** 
================ 
StoreID int(primary key),Totalsales int,BookID int (foreign key Author Bookstable(BookID)) 
================ 

I'm using Nhibernate and mapping above tables in the following 
********************* 
**Authorsdao.hdm.xml** 
********************* 
<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal"> 
    <class name="AurthosDao,Dal" table="Aurthors Table" lazy="true"> 
    <id name="AId" column="AuthorID" type="int"> 
     <generator class="native" /> 
    </id> 
    <property type="string" not-null="true" length="250" name="Authorname" column="AuthorName" /> 
    </class> 
</hibernate-mapping> 

********************* 
**Authorbooksdao.hdm.xml** 
********************* 
<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal"> 
    <class name="AurthorBooksDao,Dal" table="AuthorBookstable" lazy="true"> 
    <id name="AId" column="AuthorID"> 
     <generator class="foreign"> 
     <param name="property">AId</param> 
     </generator> 
    </id> 
    <property type="int" not-null="true" name="BookId" column="BookID" /> 
    <property type="string" not-null="true" name="Bookname" column="BookName" /> 
    </class> 
</hibernate-mapping> 


********************* 
**Booksstoredao.hdm.xml** 
********************* 
<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Dal" namespace="Dal"> 
    <class name="BookStoreDao,Dal" table="Bookstoretable" lazy="true"> 
    <id name="BookId" column="BookID"> 
     <generator class="foreign"> 
     <param name="property">BookId</param> 
     </generator> 
    </id> 
    <property type="int" not-null="true" name="StoreId" column="StoreID" /> 
    <property type="int" not-null="true" name="Totalsales" column="TotalSales" /> 
    </class> 
</hibernate-mapping> 

*************************** 
Now I have to insert the data from Asp.Net Nhibernate in to 3 tables in one transaction. 



1)I'm inserting first in Authors table after that I'm trying to get that last inserting key from Authors table and pushing into Authorsbook table 

2 )插入Autthorsbooks表後嘗試從AuthorsBook表中獲取最後插入的BookID並推入BookStore表。

如果任何幫助,這將是appreciatable ..

的Radmin我使用transcation在公立IAuthors Aurthorsdata(PlanningManagerProxy代理字符串AUTHORNAME,iBook的書籍,ILIST,IList的屬性) { AuthorDao道=新的AuthorDao();

 using (ITransaction tx = proxy.MarcomManager.GetTransaction()) 
     { 

      // Business logic of AuthorObjective 


      dao.Name = authorName; 

      tx.PersistenceManager.PlanningRepository.Save<AuthorDao>(dao); 
      tx.Commit(); 

      AuthorDao objList; 
      using (ITransaction txGet = proxy.GetTransaction()) 
      { 


       objList = txGet.PersistenceManager.PlanningRepository.Get<AuthorDao>(id); 
       txGet.Commit(); 

      } 
      var objId = from a in objList where a.AuthorID == authorId select a; 

      using (ITransaction tx2 = proxy.MarcomManager.GetTransaction()) 
      { 
       AuthorbooksDao objdao = new AuthorbooksDao(); 
       objdao.BookId = books.BookID; 
       objdao.BookName=books.BookName; 
       objdao.AuthorId= objId.AuthorId; 
       tx2.PersistenceManager.PlanningRepository.Save<AuthorbooksDao>(objdao); 
       tx2.Commit(); 

      } 
      } 
    }  

@Radim首先,我犯作者的交易,然後從AuthorDao(AuthorsTable)獲得最後插入的記錄,然後推到書店table.But同時映射時間外鍵不允許插入?

回答

0

讓我們來試試這個映射(把你的表聲明作爲一個常量)。

C#類:

public class Author 
    { 
    public virtual int ID { get; set; } 
    public virtual string Authorname { get; set; } 
    } 

    public class Book 
    { 
    public virtual int ID { get; set; } 
    public virtual Author Author { get; set; } 
    public virtual string Bookname { get; set; } 
    } 

    public class BookStore 
    { 
    public virtual int ID { get; set; } 
    public virtual Book Book { get; set; } 
    } 

XML映射:

<class name="Author" table="Authors" lazy="true"> 
    <id name="ID" column="Aid" type="int"> 
     <generator class="native" /> 
    </id> 

    <property type="string" not-null="true" length="250" name="Authorname" column="AuthorName" /> 
    </class> 

    <class name="Book" table="AuthorBooks" lazy="true"> 
    <id name="ID" column="BookID"> 
     <generator class="native" /> 
    </id> 

    <!-- reference Author --> 
    <many-to-one name="Author" column="AID" cascade="all" /> 

    <property type="string" not-null="true" name="Bookname" column="BookName" /> 
    </class> 

    <class name="BookStore" table="Bookstore" lazy="true"> 
    <id name="ID" column="StoreID"> 
     <generator class="native" /> 
    </id> 

    <!-- reference Book --> 
    <many-to-one name="Book" column="BookID" cascade="all" /> 

    <property type="int" not-null="true" name="Totalsales" column="TotalSales" /> 
    </class> 

和代碼是如何創建的作者,書和存儲項目 - 和堅持他們都:

// instances creation 
    var author = new Author { Authorname = "my author" }; 
    var book = new Book { Bookname = "my book "}; 
    var store = new BookStore { TotalSales = 1 }; 

    // references 
    book.Author = author; 
    store.Book = book; 

    // cascade all will store them all with correct ID 
    session.Save(author); 

注意:在你的片段中有很少的錯誤,所以唯一的辦法就是在綠色的場地上舉個例子d。例如。該文件應該命名爲... hbm.xml(而不是... hdm.xml,AurthosDao而不是作者Dao等

+0

感謝您的很好的解釋和code.But我試圖引用類名「作者」在書店引用獲取運行時映射錯誤。我可以知道我在哪裏做錯了嗎? –

+0

請問您是否可以擴展您的問題並顯示您的C#實體類以及錯誤的內容,stacktrace?然後我們可以做一些進一步的步驟!) –

+0

Radmin我編輯我的帖子上面,你可以請指導呢? –

相關問題