Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:54:32+00:00 2026-05-26T02:54:32+00:00

I am trying to use Fluent NHibernate. I have setup two table Products and

  • 0

I am trying to use Fluent NHibernate. I have setup two table Products and Categories.
Products has a CategoryID field and a Foreign key that ties CategoryID of Products to the PK (CategoryID) of the Categories table.

DTO: Product Class:

public class Product
{
    [HiddenInput(DisplayValue = false)]
    public virtual int ProductId { get; set; }
    public virtual string Name { get; set; }
    public virtual int CategoryId { get; set; }

    [DataType(DataType.MultilineText)]
    public virtual string Description { get; set; }
    public virtual decimal Price { get; set; }
    public virtual decimal SalePrice { get; set; }
    public virtual int StockAmt { get; set; }
    public virtual bool StockLevelWarning { get; set; }
    public virtual string Dimensions { get; set; }
    public virtual bool PriceIncludesTax { get; set; }
    public virtual string TaxClass { get; set; }
    public virtual decimal ProductWeight { get; set; }
    public virtual decimal CubicWeight { get; set; }
    public virtual string PackageDimensions { get; set; }
    public virtual bool IncludeLatestProduct { get; set; }
    public virtual Category Category { get; set; }

    public Product()
    {
        Name = String.Empty;
        Description = String.Empty;
        Price = 0m;
        SalePrice = 0m;
        StockAmt = 0;
        StockLevelWarning = true;
        Dimensions = String.Empty;
        PriceIncludesTax = false;
        TaxClass = String.Empty;
        ProductWeight = 0;
        CubicWeight = 0;
        PackageDimensions = String.Empty;
        IncludeLatestProduct = false;
    }
}

In my ProductMap class, I have a everything specified according to the Fluent Documentation including the the last property set as a Reference:

   public class ProductMap : ClassMap<Product>
{
    public ProductMap()
    {
        Table("Products");
        Id(x => x.ProductId);
        Map(x => x.Name);
        Map(x => x.CategoryId);
        Map(x => x.Description);
        Map(x => x.Price);
        Map(x => x.SalePrice);
        Map(x => x.StockAmt);
        Map(x => x.StockLevelWarning);
        Map(x => x.Dimensions);
        Map(x => x.PriceIncludesTax);
        Map(x => x.TaxClass);
        Map(x => x.ProductWeight);
        Map(x => x.CubicWeight);
        Map(x => x.PackageDimensions);
        Map(x => x.IncludeLatestProduct);
        References(x => x.Category);
    }
}

DTO: Category:

public class Category
{
    public virtual int CategoryId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual IList<Product> Products { get; set; }

    public Category()
    {
        Name = string.Empty;
        Description = string.Empty;
    }
}



public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("Categories");
        Id(x => x.CategoryId);
        Map(x => x.Name);
        Map(x => x.Description).Column("CategoryDescription");
        HasMany(x => x.Products);
    }
}

However, when I attempt to do a save on the Product entity, I get a MySQL error in return that I am trying to add the CategoryID twice. When looking at the stack trace, it specifies what columns NHibernate is attempting to save. And indeed, it lists not only CategoryID in the order that I have it specified in the ProductMap class, but also as the last column in the insert statement again.

  Error:
  "could not insert: [DTOS.Product][SQL: INSERT INTO Products (Name, CategoryId, Description, Price, SalePrice, StockAmt, StockLevelWarning, Dimensions, PriceIncludesTax, TaxClass, ProductWeight, CubicWeight, PackageDimensions, IncludeLatestProduct, Categoryid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"}

I am following the Author to Book example Fluent’s documentation uses.

It is almost as if it is equating the property (type Category) on the Product class to the Primary Key of the Category table. But as I mentioned, I am using the same example from the Fluent’s example code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:54:33+00:00Added an answer on May 26, 2026 at 2:54 am

    Above answer is correct.

    If you need categoryId then do Product.Category.Id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to define the following model where Appointment table has foreign key
I've been trying to use #harp architecture and Fluent-NHibernate. I am trying to sublass
Hi I'm trying use a datepicker on a field I have. I'm trying to
I'm trying to use the fluent mocking style of Rhino.Mocks and have the following
I'm trying to add a foreign key reference to the aspnet_users table to my
I am trying to use the repository pattern, ninject for DI with fluent nhibernate.
I was trying my hands on NHibernate and Fluent NHiberate. I wrote two classes
I am trying to use fluent nhibernate in a MVC project... i am very
I have this Fluent NHibernate mapping: public LossMap() { Table(losses); Id(x => x.Id).Column(id); References(x
I'm trying to use (new with) Fluent NHibernate (trying to switch from XML mapping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.