NHibernate映射两个表之间的多个关系 [英] NHibernate mapping multiple relationships between two tables

查看:145
本文介绍了NHibernate映射两个表之间的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个恼人的问题。我有两个在两个不同的列上相关的对象。
这些关系是相同的(都是one-Many),结构如下:

RAND table


ID
bb




$ b $ CATEGORY表 <

ID

名称

BrandID(FK to Brand。品牌名称)

DynamicBrandID(FK导航名称DynamicBrands

[其他]

但是,无论我尝试什么,我都不能在NHibernate 。HBM文件看起来像他们已经为两个对象正确生成,但无论我尝试什么,每当我想插入类别,我得到一个错误。



我的第一个问题 - 我尝试了一些NHibernate无法应付的东西?这是一个遗留数据库,所以如果是这样的话,那么我就有更大的问题。其次 - 出了什么问题?我得到了一个错误:

{此Count = 2的SqlParameterCollection的索引26无效6.} - 这是我不能再深究的了,所以我要瞎了一眼



感谢您的帮助


<虽然缺少你的映射文件,但我敢打赌,这个问题是明确的: doubled 映射。

当我们有两列表示一列时,经常会发生这样的情况:

  public virtual int BrandID {get;设置} 
公共虚拟品牌品牌{get;设置}

而xml映射看起来像:

 < property name =BrandIDcolumn =BrandID/> 

这是错误的,因为当生成SQL语句时,有两个插入到一列 BrandID 即可。但有一个解决方案,使其中一个映射只读。通常情况下, int (BrandID)是更好的选择,因为我们仍然从ORM中获益:

 < property name =BrandIDcolumn =BrandIDinsert =falseupdate =false/> 

所以,检查一下你的流利性,就像这样:

 参考文献(x => x.Brand,BrandID); 
Map(x => x.BrandID,BrandID)
.Not.Insert()
.Not.Update()
;

另请参阅在nhibernate中保存具有多对一映射的数据时出现错误


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

解决方案

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

这篇关于NHibernate映射两个表之间的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆