NHibernate Mapping problem
Posted
by Bernard Larouche
on Stack Overflow
See other posts from Stack Overflow
or by Bernard Larouche
Published on 2010-04-14T21:51:21Z
Indexed on
2010/04/14
21:53 UTC
Read the original article
Hit count: 365
My database is being driven by my NHibernate mapping files.
I have a Category class that looks like the following :
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; }
and here is my Mapping file :
<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"></key>
<one-to-many class="Category"/>
</bag>
<set name="Products" table="Categories_Products">
<key column="CategoryId"></key>
<many-to-many column="ProductId" class="Product"></many-to-many>
</set>
when I try to create the database I get the following error :
failed: The INSERT statement conflicted with the FOREIGN KEY SAME TABLE constraint "FK9AD976763BF05E2A". The conflict occurred in database "CoderForTraders", table "dbo.Categories", column 'CategoryId'. The statement has been terminated.
I looked on the net for some answers but found none. Thanks for your help
© Stack Overflow or respective owner