Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate attempts to added duplicate column on Save
    primarykey
    data
    text
    <p>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.</p> <p>DTO: Product Class: </p> <pre><code>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; } } </code></pre> <p>In my ProductMap class, I have a everything specified according to the Fluent Documentation including the the last property set as a Reference:</p> <pre><code> public class ProductMap : ClassMap&lt;Product&gt; { public ProductMap() { Table("Products"); Id(x =&gt; x.ProductId); Map(x =&gt; x.Name); Map(x =&gt; x.CategoryId); Map(x =&gt; x.Description); Map(x =&gt; x.Price); Map(x =&gt; x.SalePrice); Map(x =&gt; x.StockAmt); Map(x =&gt; x.StockLevelWarning); Map(x =&gt; x.Dimensions); Map(x =&gt; x.PriceIncludesTax); Map(x =&gt; x.TaxClass); Map(x =&gt; x.ProductWeight); Map(x =&gt; x.CubicWeight); Map(x =&gt; x.PackageDimensions); Map(x =&gt; x.IncludeLatestProduct); References(x =&gt; x.Category); } } </code></pre> <p>DTO: Category: </p> <pre><code>public class Category { public virtual int CategoryId { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual IList&lt;Product&gt; Products { get; set; } public Category() { Name = string.Empty; Description = string.Empty; } } public class CategoryMap : ClassMap&lt;Category&gt; { public CategoryMap() { Table("Categories"); Id(x =&gt; x.CategoryId); Map(x =&gt; x.Name); Map(x =&gt; x.Description).Column("CategoryDescription"); HasMany(x =&gt; x.Products); } } </code></pre> <p>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.</p> <pre><code> 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]"} </code></pre> <p>I am following the Author to Book example Fluent's documentation uses.</p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload