问题
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
回答1:
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
来源:https://stackoverflow.com/questions/24244399/nhibernate-mapping-multiple-relationships-between-two-tables