views:

29

answers:

0

I have the following class :

public class Category {

        public Category() : this("") { }

        public Category(string name) {
            Name = name;
            SubCategories = new List<Category>();
            Products = new HashSet<Product>();
        }


        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual Category Parent { get; set; }
        public virtual bool IsDefault { get; set; }
        public virtual ICollection<Category> SubCategories { get; set; }
        public virtual ICollection<Product> Products { get; set; }

Each category is either a parent or belongs to a parent category. I am deriving my database from NHibernate mappling files.

Now I don't know how to map the Parent Category property that belongs to the Category class. In the database I would like to have a ParentId column which would be a Guid related to the Parent Category and not a Category because I would be unable to delete a Category.

Here is my mapping file :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CBL.CoderForTraders.DomainModel" namespace="CBL.CoderForTraders.DomainModel" default-access="field.camelcase-underscore" default-lazy="true">

  <class name="Category" table="Categories" >
    <id name="_persistenceId" column="CategoryId" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000">
      <generator class="assigned" />
    </id>
    <version name="_persistenceVersion" column="RowVersion" access="field" type="int" unsaved-value="0" />

    <property name="Name" column="Name" type="String" not-null="true"/>
    <property name="IsDefault" column="IsDefault" type="Boolean" not-null="true" />
    <property name="Description" column="Description" type="String" not-null="true" />

    <many-to-one name="Parent" column="ParentID"></many-to-one>

    <bag name="SubCategories" inverse="true">
      <key column="ParentID" foreign-key="FK_ParentCategory_Category" />
      <one-to-many class="Category"/>
    </bag>
    <bag name="Products" table="Categories_Products">
      <key column="CategoryId" foreign-key="FK_CategoryProducts" />
      <many-to-many column="ProductId" class="Product" foreign-key="FK_ProductCategories"></many-to-many>
    </bag>
  </class>
</hibernate-mapping>

Is there a way to map the Parent Category property to a column of type Guid ?

I tried to include a type="Guid" to this line :

<key column="ParentID" foreign-key="FK_ParentCategory_Category" />

It didn't work

related questions