I have the following class in my project
public class ProductCategory { public virtual Guid Id { get; set; } public virtual string UrlSlug { get; set; } public virtual string Title { get; set; } public virtual bool IsActive { get; set; } public virtual IList<Product> Products { get; set; } public virtual ProductCategory Parent { get; set; } public virtual IList<ProductCategory> Categories { get; set; } }
my database table is as follows:
CREATE TABLE [dbo].[ProductCategory]( [Id] [uniqueidentifier] NOT NULL, [UrlSlug] [nvarchar](255) NULL, [Title] [nvarchar](255) NULL, [IsActive] [bit] NULL, [ProductCategory_id] [uniqueidentifier] NULL -- this is the parent category id )
I am trying to allow categories to be a child of another, and obviously a parent can have multiple categories.
I'm having problems getting my AutoPersistenceModel to work. This is what I have for my display.
.ForTypesThatDeriveFrom(map => { map.HasMany(productCategory => productCategory.ProductCategories).WithForeignKeyConstraintName("ProductCategory_id"); map.HasOne(productCategory => productCategory.ParentCategory).WithForeignKey("ProductCategory_id"); });
I tried several different things that just didn't work for me. HasMany seems to display correctly. But HasOne turns out to be itself, and not the correct parent or nothing (null) when ParentCategory_id is null in the database
source share