2013-07-13 91 views
0

我正在使用NHibernate的3.3.2.4(不流利)......我試圖將數據庫模型映射到應用程序模型,我即將脫鉤。如何將我的數據庫映射到我的模型?

我有以下的數據庫結構(簡化爲簡潔起見):

LibraryItems 
------------ 
Id UniqueIdentifier Primary Key, 
UserId UniqueIdentifier Foreign Key References Users(UserId), 
Type SmallInt,    --Book, Magazine etc. 
ProductId UniqueIdentifier --Links to UserBooks When Type = 1 
          --   UserMagazines When Type = 2 

UserBooks 
--------- 
ProductId UniqueIdentifier Unique Foreign Key References Books(Id), 
--Other fields pertaining to books 

UserMagazines 
------------- 
ProductId UniqueIdentifier Unique Foreign Key References Magazines(Id), 
--Other fields pertaining to magazines 

UserBookmarks 
------------- 
Id UniqueIdentifier Primary Key, 
LibraryItemId UniqueIdentifier Foreign Key References LibraryItems(Id), 
BookmarkLocation NVarChar(100) 

我也有以下應用模式

public enum LibraryItemType 
{ 
    Book = 1, 
    Magazine = 2 
} 

public abstract class LibraryItem 
{ 
    public Guid Id { get; set; } //ProductId 
    public Guid UserId { get; set; } 
    abstract public LibraryItemType Type { get; } 
} 

public abstract class ReadableLibraryItem : LibraryItem 
{ 
    public Bookmark CurrentPosition { get; set; } 
} 

public class UserBook : ReadableLibraryItem 
{ 
    public override LibraryItemType Type { get { return LibraryItemType.Book; } } 
    //Other properties pertaining to books 
} 

public class UserMagazine : ReadableLibraryItem 
{ 
    public override LibraryItemType Type { get { return LibraryItemType.Magazine; } } 
    //Other properties pertaining to magazines 
} 

我完全不知所措如何從地圖我的數據庫中的數據模型到應用程序模型...迄今爲止,我有:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping default-cascade="save-update" 
        assembly="Model" 
        namespace="Model.Library" 
        default-lazy="false" xmlns="urn:nhibernate-mapping-2.2"> 

    <class name="LibraryItem" table="LibraryItems" optimistic-lock="version"> 

    <id name="Id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000"> 
     <generator class="assigned"/> 
    </id> 

    <discriminator column="Type" type="Int32" /> 
    <property name="UserId" type="Guid" not-null="true" /> 
    <property name="ProductId" type="Guid" not-null="true" /> 
    <property name="Type" type="Model.Library.LibraryItemType, Model" not-null="true" /> 

    <joined-subclass name="ReadableLibraryItem"> 

     <one-to-one name="CurrentPosition" type="Model.Library.Bookmark" class="Bookmark" property-ref="LibraryItemId" /> 

     <joined-subclass name="UserBook" table="UserBooks" lazy="false" discriminator-value="1"> 
     <key column="Id" /> 
     </joined-subclass> 

     <joined-subclass name="UserMagazineIssue" table="UserMagazineIssues" lazy="false" discriminator-value="2"> 
     <key column="Id" /> 
     </joined-subclass> 

    </joined-subclass> 

    </class> 
</hibernate-mapping> 

我的問題似乎是雙重的。

  1. 我在我的應用程序模型中的子類UserBooks和UserMagazines實際上是SubTypes的子類型,我不知道如何在我的hbm文件中建模。
  2. 鑑別器位於LibraryItems表中,但應用於子子類中。

回答

0

它一直以來我創建映射手動一段時間,但在過去,我已經使用映射發電機像這樣的:http://nmg.codeplex.com/

是NHibernate的(正如EF)重要從表到類提供映射。如果您有子類型,我會將它們視爲單獨的類,並在您的映射中添加默認的「where」子句。

所以,你將有3個映射文件的子類型中的每一個:

  • ReadableLibraryItem
  • UserBook
  • UserMagazine

再次,它已經因爲我已經有一段時間我已經完成了手動映射,但這就是我如何去做的。

還有一件事,它曾經是所有的屬性都需要「虛擬」,我不確定這是否仍然如此。

希望這會有所幫助,

相關問題