Friday, April 22, 2011

Debugging Fluent NHibernate mappings

Debugging NHibernate is tricky sometimes. It's confusing if you're looking in the wrong places. If you have this kind of error...

Could not determine type for: JqueryAjaxComboBoxAspNetMvcHelperDemo.Models.Product, JqueryAjaxComboBoxAspNetMvcHelperDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(ProductId)

...don't be stucked at your Product and ProductMap class, find (Ctrl+F) this: Column("ProductId")

So in our code, the problem lies in (line 57): Map(x => x.Product).Column("ProductId");, must change that to References(x => x.Product).Column("ProductId")

public class Product
{
    public virtual int ProductId { get; set; }
    public virtual string ProductCode { get; set; }
    public virtual string ProductName { get; set; }
    public virtual Category Category { get; set; }
}

public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Id(x => x.ProductId);
        Map(x => x.ProductCode);
        Map(x => x.ProductName);
        References(x => x.Category).Column("CategoryId");                
    }

}



public class Category
{
    public virtual int CategoryId { get; set; }
    public virtual string CategoryCode { get; set; }
    public virtual string CategoryName { get; set; }
    public virtual int Ranking { get; set; }
}

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Id(x => x.CategoryId);
        Map(x => x.CategoryCode);
        Map(x => x.CategoryName);
        Map(x => x.Ranking);
    }
}

    
public class Purchased
{
    public virtual int PurchasedId { get; set; }
    public virtual Product Product { get; set; }
    public virtual int Quantity { get; set; }
    public virtual string PurchasedBy { get; set; }
}

    
public class PurchasedMap : ClassMap<Purchased>
{
    public PurchasedMap()
    {
        Id(x => x.PurchasedId);
        Map(x => x.Product).Column("ProductId");
        Map(x => x.Quantity);
        Map(x => x.PurchasedBy);
    }
}

No comments:

Post a Comment