0

I have an irritating problem. I've got two objects which are related on two different columns. The relationships are the same (both one-Many) and the structure is as follows

BRAND table

ID
Name
[Other stuff]

CATEGORY table

ID
Name
BrandID (FK to Brand. Navigation Name Brands)
DynamicBrandID (FK to Brand. Navigation Name DynamicBrands
[Other stuff]

But no matter what I try, I can't get this to work in NHibernate. The HBM files look like they've generated correctly for both objects but no matter what I try, every time I want to insert into category I get an error.

My first question - Am I trying something that NHibernate cannot cope with? It's a legacy database so if that's the case, then I have bigger problems.

Secondly - What's going wrong? I get an error along the lines of :

{"Invalid index 26 for this SqlParameterCollection with Count=26."} - which I can't dig into any more at all so am flying a little blind

thanks for your help

Carl Sargunar
  • 577
  • 1
  • 5
  • 15
  • Could you please complement your question with your mapping file (or mapping class)? – J.Hudler Jun 16 '14 at 13:57
  • There's a lot of cruft on the models which isn't related to the question as I can see - I didn't want to confuse things,but if you really want to see them - https://dl.dropboxusercontent.com/u/17868024/model.zip – Carl Sargunar Jun 16 '14 at 14:03

1 Answers1

0

While there is a lack of your mapping files, I would bet that the issue is clear: doubled mapping. It very often happens when we do have two represenations of one column:

public virtual int   BrandID { get; set }
public virtual Brand Brand   { get; set }

And the xml mapping looks like:

<property    name="BrandID" column="BrandID" /> 
<many-to-one name="Brand"   column="BrandID" class="Brand" />

And this is wrong, because when SQL statement is generated, there are two inserts into one column "BrandID". But there is a solution, make one of these mappings readonly. Usually the int (BrandID) is the better choice, because we still profit from ORM:

<property    name="BrandID" column="BrandID" insert="false" update="false"/> 
<many-to-one name="Brand"   column="BrandID" class="Brand" />

So, check your fluent and make it like this:

References(x => x.Brand, "BrandID");
Map(x => x.BrandID, "BrandID")
  .Not.Insert()
  .Not.Update()
  ;

Also check Error is coming while saving data with many to one mapping in nhibernate

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • What's the point of mapping BrandId explicitly above. Why not just access it via `Brand.Id`. – Cole W Jun 17 '14 at 15:51