为什么nhibernate 不能保存on-to-many的结构
下面是主类文件

Code
namespace EasyTalk.Module


{

/**//// <summary>
/// SiteAddress object for NHibernate mapped table 'SiteAddress'.
/// </summary>
[Serializable]
public class SiteAddress

{

Member Variables#region Member Variables
protected string _siteid;
protected string _alias;
protected string _address;
protected string _transporttype;
protected string _description;
protected string _iscurrent;
protected IList<AddressAttributes> _addressattributes;
#endregion

Constructors#region Constructors

public SiteAddress()
{}
public SiteAddress(string siteid, string alias, string address, string transporttype, string description, string iscurrent)

{
this._siteid= siteid;
this._alias= alias;
this._address= address;
this._transporttype= transporttype;
this._description= description;
this._iscurrent= iscurrent;
}

public SiteAddress(string siteid)

{
this._siteid= siteid;
}
#endregion

Public Properties#region Public Properties
public virtual string SiteId

{

get
{ return _siteid; }

set
{_siteid= value; }
}
public virtual string Alias

{

get
{ return _alias; }

set
{_alias= value; }
}
public virtual string Address

{

get
{ return _address; }

set
{_address= value; }
}
public virtual string TransportType

{

get
{ return _transporttype; }

set
{_transporttype= value; }
}
public virtual string Description

{

get
{ return _description; }

set
{_description= value; }
}
public virtual string IsCurrent

{

get
{ return _iscurrent; }

set
{_iscurrent= value; }
}
public virtual IList<AddressAttributes> AddressAttributes

{

get
{ return _addressattributes; }

set
{_addressattributes= value; }
}

public virtual void AddItem(AddressAttributes item)

{
if (_addressattributes == null)
_addressattributes = new List<AddressAttributes>();
_addressattributes.Add(item);
}
#endregion

Equals And HashCode Overrides#region Equals And HashCode Overrides

/**//// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )

{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
SiteAddress castObj = (SiteAddress)obj;
return ( castObj != null ) &&
this._siteid == castObj.SiteId;
}

/**//// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()

{
int hash = 57;
hash = 27 * hash * _siteid.GetHashCode();
return hash;
}
#endregion
}
}
下面是明细

Code
namespace EasyTalk.Module


{

/**//// <summary>
/// AddressAttributes object for NHibernate mapped table 'AddressAttributes'.
/// </summary>
[Serializable]
public class AddressAttributes

{

Member Variables#region Member Variables
protected decimal _attrid;
protected string _attributekey;
protected string _attributevalue;
protected string _endpointid;
protected string _transporttype;
#endregion

Constructors#region Constructors

public AddressAttributes()
{}
public AddressAttributes(string attributekey, string attributevalue, string endpointid, string transporttype)

{
this._attributekey= attributekey;
this._attributevalue= attributevalue;
this._endpointid= endpointid;
this._transporttype= transporttype;
}

public AddressAttributes(string endpointid)

{
this._endpointid= endpointid;
}
#endregion

Public Properties#region Public Properties
public virtual decimal AttrId

{

get
{ return _attrid; }

set
{_attrid= value; }
}
public virtual string AttributeKey

{

get
{ return _attributekey; }

set
{_attributekey= value; }
}
public virtual string AttributeValue

{

get
{ return _attributevalue; }

set
{_attributevalue= value; }
}
public virtual string EndpointId

{

get
{ return _endpointid; }

set
{_endpointid= value; }
}
public virtual string TransportType

{

get
{ return _transporttype; }

set
{_transporttype= value; }
}
#endregion

Equals And HashCode Overrides#region Equals And HashCode Overrides

/**//// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )

{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
AddressAttributes castObj = (AddressAttributes)obj;
return ( castObj != null ) &&
this._attrid == castObj.AttrId;
}

/**//// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()

{
int hash = 57;
hash = 27 * hash * _attrid.GetHashCode();
return hash;
}
#endregion
}
}
配置文件

Code
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--Build: with lujan99@usa.net Nhibernate template-->
<class name="EasyTalk.Module.SiteAddress,EasyTalk.Module" table="SiteAddress" lazy="true">
<id name="SiteId" column="Site_ID" type="string" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="Alias" column="Alias" type="string" />
<property name="Address" column="Address" type="string" />
<property name="TransportType" column="TransportType" type="string" />
<property name="Description" column="Description" type="string" />
<property name="IsCurrent" column="isCurrent" type="string" />
<bag name="AddressAttributes" lazy="true" cascade="all">
<key column="Endpoint_ID" />
<one-to-many class="EasyTalk.Module.AddressAttributes,EasyTalk.Module" />
</bag>
</class>
</hibernate-mapping>

Code
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--Build: with lujan99@usa.net Nhibernate template-->
<class name="EasyTalk.Module.AddressAttributes,EasyTalk.Module" table="AddressAttributes" lazy="true">
<id name="AttrId" column="Attr_ID" type="Decimal" unsaved-value="0">
<generator class="native" />
</id>
<property name="AttributeKey" column="AttributeKey" type="string" />
<property name="AttributeValue" column="AttributeValue" type="string" />
<property name="EndpointId" column="Endpoint_ID" type="string" a="true" />
<property name="TransportType" column="TransportType" type="string" />
</class>
</hibernate-mapping>

Code
AddressAttributes s = new AddressAttributes();
//s.Endpoints = e;
//s.AttrId = 1002;
s.EndpointId = "100";
s.AttributeKey = "test";
s.AttributeValue = "test";
s.TransportType = "msmq";
//=====================
//a.Endpoints.Add(e);
a.AddItem(e);
//e.AddressAttributes.Add(s);
e.AddItem(s);

SiteAddress sa = new SiteAddress();
sa.Address = "add";
//sa.AddItem(s);
sa.Alias = "alias";
sa.Description = "desc";
sa.IsCurrent = "1";
sa.SiteId = "100";
sa.TransportType = "msmq";

session.Save(sa);














































































































































































下面是明细











































































































































配置文件



































代码


























保存也没有提示错误,但是就是保存不了数据,我单个保存主表也不行,单个保存明细表可以。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix